using delete operator
-
let me explain with an example:
BYTE* pbyte1 = new BYTE();
delete [] pbyte1;The second line is suppsed to be just :
delete pbyte1;
So herez my question: is there anything wrong with using the '[]' alongside teh delete operator even though the dynamically created variable is not of array type?
delete [] calls the constructer for each object of the array. here you have created only one object of the BYTE. so prefer second one. Knock out 't' from can't, You can if you think you can :cool:
-
When you use [] you are deleting the array. and using the just delete pbyte you are deleting the pointer and dont forget to set the pinter to NULL Cheers "Peace of mind through Technology" -- modified at 7:14 Tuesday 13th June, 2006
thanx everybody for that quick reply!... but the question i have asked still remains unanswered! is there any problem(like mem leak, or unexpected error)when i use 'delete []' for an object that is not created as array?????
-
thanx everybody for that quick reply!... but the question i have asked still remains unanswered! is there any problem(like mem leak, or unexpected error)when i use 'delete []' for an object that is not created as array?????
See my post for the thread. delete x ; //works for a single object delete[] x; // deletes the complete array Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
See my post for the thread. delete x ; //works for a single object delete[] x; // deletes the complete array Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!
-
dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!
namaskaaram wrote:
My question is :whether there is a prob when 'delete []' is used to non-array type objects??
Do you got any problem while using delete [] ??? if so what is the problem?? Knock out 't' from can't, You can if you think you can :cool:
-
let me explain with an example:
BYTE* pbyte1 = new BYTE();
delete [] pbyte1;The second line is suppsed to be just :
delete pbyte1;
So herez my question: is there anything wrong with using the '[]' alongside teh delete operator even though the dynamically created variable is not of array type?
namaskaaram wrote:
BYTE* pbyte1 = new BYTE(); delete [] pbyte1;
IMHO, i better use Auto_ptr to deal with these problem!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!
a class’ destructor, doesn’t know what the array’s size is; (Initially delete had two arguments also.) it only knew that its sole argument is a pointer to an array. Furthermore, a programmer might mistakenly pass the wrong size of the array to delete[]. C++ creators realized this and decided to eliminate the size argument. However, they have kept the distinction between delete and delete[]. So when you use delete[] to delete the single object using delete[] p only one object gets destroyed so no problems with that else if p is an array the whole array gets destroyed. Does this answer your question now? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
namaskaaram wrote:
My question is :whether there is a prob when 'delete []' is used to non-array type objects??
Do you got any problem while using delete [] ??? if so what is the problem?? Knock out 't' from can't, You can if you think you can :cool:
well just want to know if its handled by the delete operator !!!.. just inquisitive! ;)
-
dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!
Quick answer: yes you'll have problems. new and new[] are not doing the same thing and if you mismatch that with delete and delete[], you'll be in troubles.
Cédric Moonen Software developer
Charting control -
a class’ destructor, doesn’t know what the array’s size is; (Initially delete had two arguments also.) it only knew that its sole argument is a pointer to an array. Furthermore, a programmer might mistakenly pass the wrong size of the array to delete[]. C++ creators realized this and decided to eliminate the size argument. However, they have kept the distinction between delete and delete[]. So when you use delete[] to delete the single object using delete[] p only one object gets destroyed so no problems with that else if p is an array the whole array gets destroyed. Does this answer your question now? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
finally! ...hehe..just kidding! ;) nice answer bro!.... cheerz!
-
Quick answer: yes you'll have problems. new and new[] are not doing the same thing and if you mismatch that with delete and delete[], you'll be in troubles.
Cédric Moonen Software developer
Charting controlr u sure cedric?..coz i have got a contradictory answer from an above post! ...hmm... actually i did debug!....didnt find any probz1...but i just aint quite sure if itz ok or not!.... hmmm.. :confused: -- modified at 7:43 Tuesday 13th June, 2006
-
let me explain with an example:
BYTE* pbyte1 = new BYTE();
delete [] pbyte1;The second line is suppsed to be just :
delete pbyte1;
So herez my question: is there anything wrong with using the '[]' alongside teh delete operator even though the dynamically created variable is not of array type?
When
delete []
is called, the system organizes a loop according to the number of allocated items. Within this loop, the system calls the destructor, if any. The number of iterations is got from internal values made bynew
operator. We cannot rely on the idea that thenew
operator, used in non-array manner, stores all of internal values needed bydelete []
. It depends on implementations. For instance, in my tests (VS 2003), thedelete []
statement fails if is executed for a single object allocated withnew
, and that object has a virtual destructor. In case of objects with no destructor, likeBYTE
, the system does not need to known the number of iterations, therefore thedelete []
acts likedelete
. I thinkdelete []
must be only used with arrays. Even if in some implementations the internal information allows using ofdelete []
for non-array objects, wheredelete
is expected, it will be slower, because of the loop actually executed a single time. -
Quick answer: yes you'll have problems. new and new[] are not doing the same thing and if you mismatch that with delete and delete[], you'll be in troubles.
Cédric Moonen Software developer
Charting controlhi Cedric, Can you just brief me on what problem would be there if I use delete[] to delete a single object? I just read that using the delete[] method is the best way to delete an object as it deletes the memory associated with the array or the object completely Thanks anyways I got the answer in Viorel's post Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_ -- modified at 7:50 Tuesday 13th June, 2006
-
hi Cedric, Can you just brief me on what problem would be there if I use delete[] to delete a single object? I just read that using the delete[] method is the best way to delete an object as it deletes the memory associated with the array or the object completely Thanks anyways I got the answer in Viorel's post Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_ -- modified at 7:50 Tuesday 13th June, 2006
Because delete[] expects the number of items in your array to be just before the address of the first element. This value is not present when you use new. Thus, you will have some problems because you first try to read the number of items where you shouldn't.
Cédric Moonen Software developer
Charting control -
See my post for the thread. delete x ; //works for a single object delete[] x; // deletes the complete array Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
_AnShUmAn_ wrote:
delete x ; //works for a single object delete[] x; // deletes the complete array
He's already indicated he knows the difference. Read his original question again. :rolleyes:
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
_AnShUmAn_ wrote:
delete x ; //works for a single object delete[] x; // deletes the complete array
He's already indicated he knows the difference. Read his original question again. :rolleyes:
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
and you guess what, you didn't follow what the rest of the posts were for ? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_