A Question about delete..

J

JustSomeGuy

My question is on the delete syntax

If I allocate a buffer as follows:

unsigned char * buffer = new unsigned char[10];

Then I was told I needed to delete the buffer as:

delete [] buffer;

However I was using:

delete buffer;

1) Which is correct?
2) whats the difference
3) Why didn't I detect any memory leaks?
 
K

Kai-Uwe Bux

JustSomeGuy said:
My question is on the delete syntax

If I allocate a buffer as follows:

unsigned char * buffer = new unsigned char[10];

Then I was told I needed to delete the buffer as:

delete [] buffer;

However I was using:

delete buffer;

1) Which is correct?

delete []

2) whats the difference

You have to use the [] form for delete if and only if you used the [] form
of new.
3) Why didn't I detect any memory leaks?

Because using the wrong form is undefined behavior, which does not need to
manifest itself in leaks.


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top