You are here:

C++/Delete array of pointers


I 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?

Hello 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.


- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.