Hello
I have an array of pointers. I want to take a pointer from this array and return this.
If I use new I get a segmentation fault. But when I use malloc it is ok.
Code:
SegFault
ClassEx* Example::doSomething()
{
ClassEx *array = new ClassEx[5];
fill in
return &array[2];
}
Ok
ClassEx* Example::doSomething()
{
ClassEx *array = (ClassEx*) malloc(sizeof(ClassEx) * 5);
fill in
return &array[2];
}
Can someone explain me why malloc works here and new doesn't.
Also how do I return a pointer to an element in an array using new?
Thank you