Reputation: 2037
I have an array of 6 function pointers:
int (*arrayOfFunPointers[6])();
What I want to do now, is to allocate that array in heap
Because I create the array in a function and return a pointer to that array:
int (*(*fun2())[6])(){
int (*funPointer)();
int (*arrayOfFunPointers[6])();
int (*(*pointerToArrayOfFunPointers)[6])();
funPointer = &fun;
arrayOfFunPointers[0] = funPointer;
pointerToArrayOfFunPointers = &arrayOfFunPointers;
return pointerToArrayOfFunPointers;
}
Outside of the method, the arrayOfFunPointers
lose his validity area and the returned pointer point to "nothing"
I tried a lot like:
int (*arrayOfFunPointers[6])() = new int (*[6])();
or with typedef:
typedef int (*arrayOfFunPointers[6])();
arrayOfFunPointerss *array = new arrayOfFunPointers;
or by assignment to the returned pointer:
pointerToArrayOfFunPointers = new arrayOfFunPointers;
pointerToArrayOfFunPointers = new arrayOfFunPointers[]; //or
But nothing works.
Maybe I do not understand the use of the keyword new
correctly, then it would be very helpful if anyone could tell me where I make a thinking mistake
Can anyone help me please? That would be nice
Upvotes: 0
Views: 287
Reputation: 40080
You can simplify your life with tools from C++11. What about:
std::array<FunPointer, 6> getFunctions()
{
std::array<FunPointer, 6> result;
// ...
return result;
}
isn't it just nice? Ne need to bother with dynamic memory, just copy those 6 pointers around, it is relatively cheap.
Upvotes: 3