user3918990
user3918990

Reputation: 23

pointer to class created error core dump

I'm trying to make a template class. It seems to be ok, but when I try to create a pointer to myclass' object, a core dumped error occurs.

#include <iostream>
#include <stdlib.h>

template <typename T>
class vec {
private:
    T* m_pnt;
    int m_dim;

public:
    vec();
    vec(int);
    ~vec();

    void prnt();
    void reduce(int);
    void null();

    void set(int, T);
    void print();
};

template <typename T>
vec<T>::vec() {
    m_dim = 100;
    m_pnt = (T*)calloc(100, sizeof(T));
    std::cout << "building without par" << std::endl;
}

template <typename T>
vec<T>::vec(int n) {
    m_dim = n;
    m_pnt = (T*)calloc(m_dim, sizeof(T));
    std::cout << "building" << std::endl;
}

template <typename T>
vec<T>::~vec() {
    free(m_pnt);
    std::cout << "killed" << std::endl;
}

template <typename T>
void vec<T>::reduce(int dim) {
    std::cout << "reduce" << std::endl;
    if (dim <= m_dim) {
        m_dim = dim;
        T* p = (T*)calloc(m_dim, sizeof(T));
        for (int i = 0; i != m_dim; i++)
            *(p + i) = *(m_pnt + i);

        free(m_pnt);

        m_pnt = p;
    } else {
        std::cout << "error: rowvector not valid dimension" << std::endl;
        throw;
    }
}

template <typename T>
void vec<T>::set(int idx, T val) {
    if (idx >= 1 && idx <= m_dim)
        *(m_pnt + idx - 1) = val;
    else {
        std::cout << "error: rowvector not valid index" << std::endl;
        throw;
    }
}

template <typename T>
void vec<T>::prnt() {
    for (int i = 0; i != m_dim; i++)
        std::cout << i + 1 << "\t" << m_pnt + i << "\t" << *(m_pnt + i)
                  << std::endl;
}

template <typename T>
void vec<T>::null() {
    for (int i = 1; i <= m_dim; i++)
        *(m_pnt + i - 1) = T(0);
}

int main() {
    vec<int>* d;
    std::cout << "d: " << d << std::endl;
    d->prnt();

    return 0;
}

There are no error in compiling, but the final output is something like

    d: 0
    segmentation fault (core dumped)

Upvotes: 1

Views: 613

Answers (2)

Walter
Walter

Reputation: 45434

  1. you should use C++ style allocation and de-allocation (new and delete) instead of calloc and free. This is to ensure apropriate alignment.
  2. you should not use int to hold the size of your vector, but an unsigned type. The C++ standard is to use std::size_t.
  3. you cannot expect an uninitialised pointer (d) to point to anything useful. Hence using it will cause an error (or worse: nonsensical behaviour).
  4. Have a look at any implementation of std::vector<> to see how things are done professionally.
  5. Your code is not exception safe: if allocation with calloc fails, m_dim still shows the initial dimensionality.

Upvotes: 1

fjardon
fjardon

Reputation: 7996

You never allocated your pointer:

vec<int>* d = new vec<int>;

You current code:

vec<int>* d;

Just defines a variable d holding a pointer to a vec<int> object. But it points to nowhere. To make it points to something you must build a vec<int> in memory using new vec<int>

Upvotes: 2

Related Questions