What happens if I use delete [] to a pointer from a "new char('\0')"

S

sevenever

It seems leads to memory leaking on windows + VC.
The delete will not free the memory in such condition?
 
M

Martijn van Buul

* sevenever:
It seems leads to memory leaking on windows + VC.
The delete will not free the memory in such condition?

Well, it's undefined behaviour, really. You should've used delete instead
of delete[]. delete[] should only be used for new'ed arrays.

so

char *foo = new char[100];

needs to be delete[]d, where

char *foo = new char('0');

needs to be deleted. By the way, new'ing a single char doesn't strike me
as particulary useful,
 
V

Victor Bazarov

sevenever said:
It seems leads to memory leaking on windows + VC.
The delete will not free the memory in such condition?

If you 'delete[]' a pointer NOT obtained from 'new[]', the
program has undefined behaviour. What happens in that case
is really not possible to tell in terms of C++ language.
It's just speculation, and as such is a waste of time.

V
 
S

sevenever

sevenever said:
It seems leads to memory leaking on windows + VC.
The delete will not free the memory in such condition?

If you 'delete[]' a pointer NOT obtained from 'new[]', the
program has undefined behaviour. What happens in that case
is really not possible to tell in terms of C++ language.
It's just speculation, and as such is a waste of time.

V

Thanks, I won't delete [] a pointer not obtained from new [].
The reason of this question is I am tracing a memory leaking program,
and finally located to the "delete []" line.
So, obviously I should change "delete []" to "delete".

But can i know what M$ VC8 compiler will do to this code? Just my
curiosity
 
V

Victor Bazarov

sevenever said:
[..]
The reason of this question is I am tracing a memory leaking program,
and finally located to the "delete []" line.
So, obviously I should change "delete []" to "delete".

But can i know what M$ VC8 compiler will do to this code? Just my
curiosity

You need to either look in the assembly code and trace the program
execution yourself OR ask in the VC++ newsgroup (see the hierarchy
'microsoft.public.vc.*'). Trust me, that knowledge is not going to
help you in life, only clutter your mind. Save time and brain cells.

V
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top