Question about delete a char* pointer
-
Hi, everyone! Look at the following codes, -------- const char* p = "12345"; char* q = "54321"; -------- If I want to release the memory, should I use -------- delete[] p; delete[] q; -------- or should I use -------- delete p; delete q; -------- Thanks in advance, George
-
Hi, everyone! Look at the following codes, -------- const char* p = "12345"; char* q = "54321"; -------- If I want to release the memory, should I use -------- delete[] p; delete[] q; -------- or should I use -------- delete p; delete q; -------- Thanks in advance, George
You don't need to release the memory of these variables because they aren't allocated on heap but on stack. Defining
char* q ="54321";
is the same as definingchar q[6] = "54321";
. You only need to use delete/delete [] for variables created with new/new []. -- karl -
Hi, everyone! Look at the following codes, -------- const char* p = "12345"; char* q = "54321"; -------- If I want to release the memory, should I use -------- delete[] p; delete[] q; -------- or should I use -------- delete p; delete q; -------- Thanks in advance, George
Rule of thumb: You did not call
new
, you do not need to calldelete
. Forchar* r = new char[MAX_PATH+1];
you would calldelete[] r;
since you did anew[]
. But this is one of the cases where it does not matter, sincechar
is an integral type and as such has no constructor or destructor. The difference betweendelete
anddelete[]
is thatdelete[]
calls the destructor of every item to be deleted, whereasdelete
calls only the destructor of the first item.
My opinions may have changed, but not the fact that I am right.
-
You don't need to release the memory of these variables because they aren't allocated on heap but on stack. Defining
char* q ="54321";
is the same as definingchar q[6] = "54321";
. You only need to use delete/delete [] for variables created with new/new []. -- karl -
Rule of thumb: You did not call
new
, you do not need to calldelete
. Forchar* r = new char[MAX_PATH+1];
you would calldelete[] r;
since you did anew[]
. But this is one of the cases where it does not matter, sincechar
is an integral type and as such has no constructor or destructor. The difference betweendelete
anddelete[]
is thatdelete[]
calls the destructor of every item to be deleted, whereasdelete
calls only the destructor of the first item.
My opinions may have changed, but not the fact that I am right.
-
Rule of thumb: You did not call
new
, you do not need to calldelete
. Forchar* r = new char[MAX_PATH+1];
you would calldelete[] r;
since you did anew[]
. But this is one of the cases where it does not matter, sincechar
is an integral type and as such has no constructor or destructor. The difference betweendelete
anddelete[]
is thatdelete[]
calls the destructor of every item to be deleted, whereasdelete
calls only the destructor of the first item.
My opinions may have changed, but not the fact that I am right.
jhwurmbach wrote: But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. This is true on most, but not all compilers. According to the standard, new must be deleted, and new[] delete[]'d. There is no guarantee that the compiler implements new[] "on top of" new. It could even use different heaps for that!
Nur wer feige ist tötet Liebe durch das Wort allein
[sighist] | [Agile Programming] [doxygen]
If you look for evil in me you will find it whether it's there or not. -
jhwurmbach wrote: But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. This is true on most, but not all compilers. According to the standard, new must be deleted, and new[] delete[]'d. There is no guarantee that the compiler implements new[] "on top of" new. It could even use different heaps for that!
Nur wer feige ist tötet Liebe durch das Wort allein
[sighist] | [Agile Programming] [doxygen]
If you look for evil in me you will find it whether it's there or not.