Skip to main content
GameDev.net gamedev.net
🔒 Locked

delete pointer question

Started by denuo_ Aug 23, 2006 at 9:51 AM 5 replies 800+ views
Original Post
denuo_
denuo_
hi! VC++ gives me a run-time error when i run the following code: // // my code: // // pointer to pointer A: int** ppiA = NULL; ppiA = new int*[2]; // pointer B: int* piB = NULL; piB = new int[5]; for (int i = 0; i < 5; i++) { piB = 0; } // pointer C: int* piC = NULL; piC = new int[5]; for (i = 0; i < 5; i++) { piC = 0; } ppiA[1] = piB; ppiA[2] = piC; // //... // // delete simple pointer: delete [] piC; // Ok... delete [] piB; // Ok... // delete pointer to pointer? delete [] ppiA; // run-time error... // end if i remove the "delete [] ppiA;" line, the app runs ok... Does this means that deleting the pointers "piC" and "piB", im also deleting "ppiA"? Thanks for your help.
Aardvajk
Aardvajk
Shouldn't do based on what you have posted. Are you doing anything with the pointers between the allocation and the delete statements?
dannielum
dannielum
I pasted it into my VC6++ compiler and it runs ok.
Why do you create a pointer to pointer to an array anyway? Did you try to access the array after deleting []ppiA? I believe when you delete the pointer to pointer, you actually delete both the pointers (the pointer to pointer to array, and the pointer to array). So you get access error if you try to access the array with the other pointer. I might be wrong.

Is what you trying to do is to create a pointer to an array of pointers to ints?
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
denuo_
denuo_
the code i show here is just a very very simplied version of my actual code, but i get the same run-time error... the rest is not relevant...
i need an array of pointers, but i dont know at compilation time the number of elements (pointers) i will need... the "ppiA" allocates memory for the pointers, but i cant delete it... thanks again...
Simian Man
Simian Man
The problem is these lines:

ppiA = new int*[2];
ppiA[1] = piB;
ppiA[2] = piC;

Arrays are 0-based. This meanns that if you declare an arry to have 2 elements, they are at indexes 0 and 1. Not 2.

The reason it borks on the delete is probably because assigning to ppiA[2] overwrote padding that the compiler inserts in debug mode for parity checks.
Enigma
Enigma
ppiA[<font color="red"><strike>1</strike></font> 0] = piB;ppiA[<font color="red"><strike>2</strike></font> 1] = piC;


Σnigma
denuo_
denuo_
im sooooooooooooooooo stupid :) ... of course... thanks a lot Simian_Man and the rest... please forgive me for wasting your time with these silly questions...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.