vendredi 28 avril 2017

Seg fault while assigning memory to pointer array

I get seg fault when I try to assign memory to a object pointer (trying to dynamically create a 1D array). What am I doing wrong? I first I though it must be due to the kind of type T being used, but this happens even for native data types such as int. I come from a C background (new to C++), where I would have done memory allocation using malloc/calloc.

e.g.:

template <typename T>
struct test {
   T *element;
}

class Foo {

    Foo() {
    //empty
    }
    test *t;

    template <typename T>
    void setup(int n) {
      t->element = new T[n];
    }

    void run() {
      setup(10);
    }
}

Aucun commentaire:

Enregistrer un commentaire