C++/Delete array of pointers
Expert: Eddie - 4/10/2006
QuestionI am having trouble with the syntax to delete an array of pointers in C++. I either get compile-time errors or execution-time errors. I understand that I have to delete the items pointed to by the array of pointers before I delete the actual array of pointers and that's what I'm having trouble with.
The array of pointers was dynamically allocated in a function and the address of that array was passed by reference to main. Each element of the array of pointers was also dynamically allocated in the same function. The first element is type short and the remaining 4 elements are arrays of various sizes.
Here's the code I put in main to delete the elements pointed to by the array of arrays:
StructureSize = **Ptr;
for (i = StructureSize-1; i > -1; i--)
delete [] Ptr[i];
Ptr is declared as short ** Ptr in main.
Can you help me?
AnswerHello Sam, thank you for the question.
Sure, I would be more than happy to help. I was wondering if you could sent me your actual allocation code so I can get a better understanding of what is going on. Like right now, I can't tell what a StructureSize is. Simple things like that. You can either paste the whole file, or you allocation segment. Bear in mind you could also be trying to delete memory that is being referenced by something else, which would cause a crash.
Could be any number of causes. Send me the file and I'll give it the once over.
Thanks.
- Eddie