Skip to main content
gamedev.net
🔒 Locked

delete[] vs delete

Started by jjmontes 6 replies 250+ views
Original Post
jjmontes
jjmontes
Can anyone explain please what is the difference between delete[] ptr and delete(ptr) ? Thanks. Real programers are not afraid from maths! (but I am) JJpRiVaTe (Private Zone)
-=[ J ]=-I always forget to change the tagline.
MatrixCubed
MatrixCubed
Use delete [] n in instances where you have an array of pointers to objects:


  
int* lpIntegerArray = new int[10];
delete [] lpIntegerArray;


And use delete n for single objects:


  
unsigned long* lpDoubleWord = new unsigned long;
delete lpDoubleWord;




MatrixCubed
http://MatrixCubed.cjb.net

LilBudyWizer
LilBudyWizer
The main differance is that delete[] will call the destructor for each element of the array while I''m not real sure what delete does. When I tested it it called the destructor, but when I tried accessing a data member I got an access violation.
Keys to success: Ability, ambition and opportunity.
Waverider
Waverider
If you try to access a data member after calling delete, you''ll get a violation because you are now trying to access memory that is no longer allocated to your program. (There is more that happens when you call delete than just executing the destructor)

if you use new className, then use delete
if you use new className[number], then use delete []
It's not what you're taught, it's what you learn.
LilBudyWizer
LilBudyWizer
That was accessing a member in the destructor, not after calling delete
Keys to success: Ability, ambition and opportunity.
jjmontes
jjmontes
Thanks matrixcubed. So til today I was only deleting the first instance. However I doubt, and if someone answers this...:

int *ptr_to_int;
int array_of_int[];

ptr_to_int = new int;
array_of_int = new int[40];

Now: is this correct?

delete (ptr_to_int);
delete [] array_of_int;

By the way, matrixcubed, did you receive my email answering about the limit of 1 meg? I''m not sure if I sent it ok since my ISP does whatever it wants when it wants.

Real programers are not afraid from maths! (but I am)
JJpRiVaTe (Private Zone)
-=[ J ]=-I always forget to change the tagline.
LilBudyWizer
LilBudyWizer
Well, other than that you can''t define an open array with out initilization and can''t assign to array_of_int? array_of_int needs to be a pointer as well. One points to one integer and the other points to an array of integers. Then yes it would be right although the parens are not needed.
Keys to success: Ability, ambition and opportunity.
Landsknecht
Landsknecht
If you change your code to this:

int *ptr_to_int;
int *array_of_int;

ptr_to_int = new int;
array_of_int = new int[40];

delete (ptr_to_int);
delete [] array_of_int;


Then, yes, that is correct.

BTW - why do you insist on putting ptr_to_int inside parantheses? It is not needed and seems like extra shift key typing to me.


Landsknecht
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.

Topic Locked

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

Sign in to reply to this topic.