By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

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

P: n/a
It seems leads to memory leaking on windows + VC.
The delete will not free the memory in such condition?

Sep 6 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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,

--
Martijn van Buul - pi**@dohd.org
Sep 6 '07 #2

P: n/a
sevenever wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #3

P: n/a
On Sep 6, 10:45 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
sevenever wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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

Sep 6 '07 #4

P: n/a
sevenever wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.