delete and delete []

S

SpreadTooThin

I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
 
M

Mike Wahler

SpreadTooThin said:
I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;
Correct.

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

No, you must still use delete[]. It's still an
array (albeit whose objects are of a different type).
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)

The array's element type has no bearing on this.
If you use 'new', you must deallocate with 'delete'.
If you use 'new[]', you must deallocate with 'delete[]'.
Simple, huh?

-Mike
 
D

dave_mikesell

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

No, not irrelevant. If you allocate with [], delete with [].
 
V

Victor Bazarov

SpreadTooThin said:
I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

No, of course not. Why do you think there might be a difference?
It's an array. You use 'new[]' to allocate it, you use 'delete[]'
to free it.
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)

No, you can't change the object type. That's nonsense, sorry.

V
 
G

Gavin Deane

I understand that if you allocate an array using new as follows

object * b = new object[n];

then you must free the object as follows:

delete [] b;

however if you are allocating a char buffer as follows:

char * b = new char [n];

then isn't it irrelavant if you use

delete b or delete [] b ?

Who told you that? It certainly is relevant. The behaviour of mixing
new [] with delete is undefined.
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)

As ever with undefined behaviour, whether you have issues, what those
issues are, whether they are different for different types etc. is
outside the scope of the language definition.

Gavin Deane
 
J

James Curran

however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?

You seem to be misunderstanding what delete & delete[] do.

basically, they functions like:
delete pObject; =

pObject->~object();
free_n_bytes_of_memory(pObject, sizeof(object));


delete[] arrObject; =

for(int i =0; i < N; ++i)
arrObject.~object();
free_n_bytes_of_memory(arrObject, sizeof(object)* N );


Nw, you are correct in surmizing that, as a char dtor is empty, the
different in the first part of each is irrelevent. However, the
difference in the second half is still significant.

To further confuse the issue, on most platforms,
free_n_bytes_of_memory could probably be implemented as:

void free_n_bytes_of_memory(void* ptr, int )
{
free(ptr);
}

with the size parameter ignored, but that would be an implementation
detail, which you can't and shouldn't depend on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top