#include "stdafx.h"
#include <iostream.h>
int main(int argc, char* argv[])
{
int* ptest[2];
ptest[0] = new int;
ptest[1] = new int;
*ptest[0] = 99;
cout << *ptest[0] << endl;
delete [] ptest;
return 0;
}
Why can't I delete my array?
Ok, I''ve been having problems trying to get the delete[] operator to work properly in my tetris game. So I whipped up a quick little Dos program to test some things out and I''m getting the same thing. My little program below works fine if I do an delete of just a non-array pointer but as soon as I make it an array I get this error message. And it dosn''t matter if I put in the square brackets either. It gives me the same error either way.
debug assertion failure.
Program: My program path
File: dbgdel.cpp
Line: 47
Expression: _BLOCK_TYPE_IS_INVALID(pHead->nBlockUse)
All the books I''ve read say to delete an array of pointers, just add the [] after the delete command. What am I doing wrong????
Quiggy.
quote: Original post by Quiggy
Ok, I've been having problems trying to get the delete[] operator to work properly in my tetris game. So I whipped up a quick little Dos program to test some things out and I'm getting the same thing. My little program below works fine if I do an delete of just a non-array pointer but as soon as I make it an array I get this error message. And it dosn't matter if I put in the square brackets either. It gives me the same error either way.
debug assertion failure.
Program: My program path
File: dbgdel.cpp
Line: 47
Expression: _BLOCK_TYPE_IS_INVALID(pHead->nBlockUse)
All the books I've read say to delete an array of pointers, just add the [] after the delete command. What am I doing wrong????
Quiggy.
Two problems with your code.
1. You have a statically defined array of pointers to int. You cannot delete an array you didn't create dynamically with new.
2. You call new twice on each member of the array, therefore you'd need to call delete on each member. One new, one delete. Two news, two deletes.
I think you're confused about what your array holds.
If you want an array of ints, do this:
// allocate the arrayint *arr = new int[2];arr[0] = 99;arr[1] = 88;// delete the arraydelete [] arr;
If you want a dynamically created array of pointers to int, then do this:
// allocate the arrayint **arr = new int*[2];// allocate each pointer in the arrayarr[0] = new int;arr[1] = new int;*arr[0] = 99;*arr[1] = 88;// delete the two pointers in the arraydelete arr[0];delete arr[1];// delete the arraydelete [] arr;
Hope that helps.
[edited by - fizban75 on February 6, 2003 1:44:18 AM]
Yup, thats seems to have fixed that. Sorta. I still can''t get the delete name[] operator working, but I think thats because I am creating new classes and not just a data type. So for now I just have a for loop creating them seperately. Seems to work for now. I''ll play with it some more and see if I can get it going.
Thanks for the explanation on delete by the way. You should have written the chapters on the delete operator in the books I was reading.
Thanks again.
Quiggy
Thanks for the explanation on delete by the way. You should have written the chapters on the delete operator in the books I was reading.
Thanks again.
Quiggy
quote: Original post by Quiggy
Yup, thats seems to have fixed that. Sorta. I still can''t get the delete name[] operator working, but I think thats because I am creating new classes and not just a data type. So for now I just have a for loop creating them seperately. Seems to work for now. I''ll play with it some more and see if I can get it going.
Thanks for the explanation on delete by the way. You should have written the chapters on the delete operator in the books I was reading.
Thanks again.
Quiggy
No problem. Glad to help.
If you show me the code you use to create and delete your array of classes, I can help out there too. There should be no difference between allocating a dynamic array of built-in types (ints, floats, etc.) and user-defined types (classes and structs).
Here''s an example:
class A {};int main(int argc, char **argv) { // allocate an array of 5 A''s A *a1 = new A[5]; // Now we have 5 instances of class A in our array that // were constructed with the default constructor // and we can now use them anyway we want // Once we''re done with the array, we should delete it delete [] a1; // Now we want to allocate an array of *pointers* to A A **a2 = new A*[5]; // We can now loop through the array of pointers to A and // allocate each one for (int i=0; i<5; ++i) { a2[i] = new A; } // Once we''re done with the array of pointers, we need to // loop through each item and delete it for (int j=0; j<5; ++j) { delete a2[j]; } // Then we have to delete the array itself! delete [] a2; // That''s it! return 0;}
quote: Original post by Quiggy
Yup, thats seems to have fixed that. Sorta. I still can''t get the delete name[] operator working, but I think thats because I am creating new classes and not just a data type. So for now I just have a for loop creating them seperately. Seems to work for now. I''ll play with it some more and see if I can get it going.
You said delete name[]. Did you mean delete [] name?
Yeah, the above message must have been a typo.
Anyways, I''ve had a chance to check out your code fizban75, but am unable to adapt it to my program. I can''t even get the thing to compile. Let me show you what I have. I do things a bit different then you have in your example.
First of all I init the pointer ''m_pFallingPiece'' in my main.h file. This is in the private declaration of my CGame Class. You can ignore the m_pPlayArea[][]. I just include it for completeness, just in case...
Next I use the ''new'' command within the Game.cpp file like so. I create each array seperatly and modifiy each one in turn.
Then I do stuff within my gameloop which dosn''t apply to my problem. Finally the game is over and it''s time to clean everything up nice and neat. The following code is in my destructor of the CGame class.
Everything works fine for the most part if I discard the delete lines, but as soon as I put them in I get the assert errors again. I''m also not to sure if thats the correct way to delete a 2 dimensional array. I tried using 2 set of brackets ( [][] ), but that came up with a compile error.
So basically whenever I use the delete operator, I get problems. It works fine if I don''t use them. Hopefully your able to understand this and help me out. It''s stumped me.
Quiggy
Anyways, I''ve had a chance to check out your code fizban75, but am unable to adapt it to my program. I can''t even get the thing to compile. Let me show you what I have. I do things a bit different then you have in your example.
First of all I init the pointer ''m_pFallingPiece'' in my main.h file. This is in the private declaration of my CGame Class. You can ignore the m_pPlayArea[][]. I just include it for completeness, just in case...
CObject* m_pFallingPiece[2]; // The actual 3 block piece that falls. CObject* m_pPlayArea[PLAYAREA_HEIGHT][PLAYAREA_WIDTH];
Next I use the ''new'' command within the Game.cpp file like so. I create each array seperatly and modifiy each one in turn.
void CGame::CreateStartBlocks(){ // Lets create the GamePiece // Since the game piece 3 long. // [0] Block1 // [1] Block2 // [2] Block3 // Block 1 m_pFallingPiece[0] = new CObject(m_pD3DDevice); m_pFallingPiece[0]->SetSize(16, 16, 1); m_pFallingPiece[0]->SetPosition(INIT_FALL_POS_X, INIT_FALL_POS_Y, 0); // Random select the texture for the plane. Rand_Tex(0); // Block 2 m_pFallingPiece[1] = new CObject(m_pD3DDevice); m_pFallingPiece[1]->SetSize(16, 16, 1); m_pFallingPiece[1]->SetPosition(INIT_FALL_POS_X, (INIT_FALL_POS_Y - BLOCK_HEIGHT), 0); Rand_Tex(1); // Block 3 m_pFallingPiece[2] = new CObject(m_pD3DDevice); m_pFallingPiece[2]->SetSize(16, 16, 1); m_pFallingPiece[2]->SetPosition(INIT_FALL_POS_X, (INIT_FALL_POS_Y - (BLOCK_HEIGHT * 2)), 0); Rand_Tex(2); iSpacesDown = SPACESDOWN_START; // I start just above the grid. iSpacesAcross = SPACESACROSS_START; // I start 4 spaces into the playarea.}
Then I do stuff within my gameloop which dosn''t apply to my problem. Finally the game is over and it''s time to clean everything up nice and neat. The following code is in my destructor of the CGame class.
delete m_pFallingPiece [0]; delete m_pFallingPiece [1]; delete m_pFallingPiece [2]; for (int h=0; h < PLAYAREA_HEIGHT; h++) { for (int w=0; w < PLAYAREA_WIDTH; w++) delete m_pPlayArea[h][w]; } delete [] m_pFallingPiece; delete [] m_pPlayArea;
Everything works fine for the most part if I discard the delete lines, but as soon as I put them in I get the assert errors again. I''m also not to sure if thats the correct way to delete a 2 dimensional array. I tried using 2 set of brackets ( [][] ), but that came up with a compile error.
So basically whenever I use the delete operator, I get problems. It works fine if I don''t use them. Hopefully your able to understand this and help me out. It''s stumped me.
Quiggy
February 07, 2003 06:49 PM
CObject* m_pFallingPiece[2];
CObject* m_pPlayArea[PLAYAREA_HEIGHT][PLAYAREA_WIDTH];
// try this out instead
CObject* m_pFallingPiece; // The actual 3 block piece that falls.
CObject** m_pPlayArea;
m_pFallingPiece = new CObject[2];
m_pPlayArea = new CObject[PLAYAREA_HEIGHT][PLAYAREA_WIDTH];
delete[] m_pFallingPiece;
delete[] m_pPlayArea;
CObject* m_pPlayArea[PLAYAREA_HEIGHT][PLAYAREA_WIDTH];
// try this out instead
CObject* m_pFallingPiece; // The actual 3 block piece that falls.
CObject** m_pPlayArea;
m_pFallingPiece = new CObject[2];
m_pPlayArea = new CObject[PLAYAREA_HEIGHT][PLAYAREA_WIDTH];
delete[] m_pFallingPiece;
delete[] m_pPlayArea;
yeah...what the AP said should work.
Programmers of the world, UNTIE!
yeah...what the AP said should work.
Programmers of the world, UNTIE!
Quiggy, you don''t need to use the last two delete operators in your code. That''s what your problem is.
Count how many news you do. You should have exactly the same number of deletes.
You''re not allocating any arrays with new. You''re only allocating the array members. Therefore, you only need to delete the array members and you don''t need to delete the arrays themselves.
More specifically:
There is no "new" here. This is a statically declared array. You don''t need to call delete on it. However, if you did this instead:
then you would need to call delete on it, like this:
Understand?
Count how many news you do. You should have exactly the same number of deletes.
You''re not allocating any arrays with new. You''re only allocating the array members. Therefore, you only need to delete the array members and you don''t need to delete the arrays themselves.
More specifically:
CObject* m_pFallingPiece[2];
There is no "new" here. This is a statically declared array. You don''t need to call delete on it. However, if you did this instead:
CObject **m_pFallingPiece;m_pFallingPiece = new CObject*[2];
then you would need to call delete on it, like this:
delete [] m_pFallingPiece
Understand?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement