C++ memory freeing
-
I am a little confused about when to use delete and delete[]. Is this correct?
int* numArray = new int[100];
delete numArray;Or should it be this?
int* numArray = new int[100];
delete[] numArray;I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.
-
I am a little confused about when to use delete and delete[]. Is this correct?
int* numArray = new int[100];
delete numArray;Or should it be this?
int* numArray = new int[100];
delete[] numArray;I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.
-
I am a little confused about when to use delete and delete[]. Is this correct?
int* numArray = new int[100];
delete numArray;Or should it be this?
int* numArray = new int[100];
delete[] numArray;I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.
Array delete operator,
delete[]
invokes the destructor of each array element before freeing the array. For an array of integers, it doesn't make any difference. Meanwhile, if the objects in the array have a non-trivial destructor you should call thedelete[]
operator.Mircea
-
I am a little confused about when to use delete and delete[]. Is this correct?
int* numArray = new int[100];
delete numArray;Or should it be this?
int* numArray = new int[100];
delete[] numArray;I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.
As noted you should use the correct one. I know for a fact that long ago using the wrong one would cause memory stack corruption. I presume that now that isn't possible or is less likely. Although I would be curious if the spec deals with that specific issue.
-
As noted you should use the correct one. I know for a fact that long ago using the wrong one would cause memory stack corruption. I presume that now that isn't possible or is less likely. Although I would be curious if the spec deals with that specific issue.
In any case, smart pointers are preferable in almost all cases to new/delete. I imagine that there are some cases where new and delete are preferred to smart pointers, but for most applications, a smart pointer is the better option.
"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
Array delete operator,
delete[]
invokes the destructor of each array element before freeing the array. For an array of integers, it doesn't make any difference. Meanwhile, if the objects in the array have a non-trivial destructor you should call thedelete[]
operator.Mircea
Mircea Neacsu wrote:
For an array of integers, it doesn't make any difference.
It is true that no destructor is called for each element of an int[] array, but that does not mean that the memory layout is compatible with delete. For example, the compiler might allocate additional space for a variable containing the size of the array and return a pointer to memory after this variable. When using delete, the heap will be corrupted. The heap layout is implementation-dependent, and therefore you should always match new/delete and new[]/delete[]!
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
-
Mircea Neacsu wrote:
For an array of integers, it doesn't make any difference.
It is true that no destructor is called for each element of an int[] array, but that does not mean that the memory layout is compatible with delete. For example, the compiler might allocate additional space for a variable containing the size of the array and return a pointer to memory after this variable. When using delete, the heap will be corrupted. The heap layout is implementation-dependent, and therefore you should always match new/delete and new[]/delete[]!
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
While you are absolutely right, I don't know of any heap manager that behaves the way you describe. Let's say that calling
delete
on anint
array is a smaller sin, a "peccadillo" that will place you on one of the first circles of hell. :laugh:Mircea