Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Same variable names inside multiple sub-scopes results in delete tocrash sporadically

Reply
Thread Tools

Same variable names inside multiple sub-scopes results in delete tocrash sporadically

 
 
Juha Nieminen
Guest
Posts: n/a
 
      10-06-2012
Stefan van Kessel <(E-Mail Removed)> wrote:
> On 2012-10-05 11:39 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> char* aPtr = new char[1024];
>> delete aPtr;
>>
>> The delete2 crashes sometimes.

>
> When you use new ... [] you need to use delete[] ...


For that matter, there's very seldom any need to use 'new' and 'delete'
directly in your code, unless you are implementing a special dynamic data
container (and even that happens quite rarely in practice, as it's not
very common to need a data container that's different from what the
standard library offers).

The lesson is: Only use 'new' and 'delete' if you absolutely must. Before
resorting to them, see if the standard containers will do what you want
in the way you want. It will not only be safer and less error-prone, it
will also be much simpler and your code will become clearer and easier
to read.
 
Reply With Quote
 
 
 
 
Single Stage to Orbit
Guest
Posts: n/a
 
      10-06-2012
On Sat, 2012-10-06 at 11:24 +0000, Juha Nieminen wrote:
> Stefan van Kessel <(E-Mail Removed)> wrote:
> > On 2012-10-05 11:39 AM, (E-Mail Removed) wrote:
> >> char* aPtr = new char[1024];
> >> delete aPtr;
> >>
> >> The delete2 crashes sometimes.

> >
> > When you use new ... [] you need to use delete[] ...

>
> For that matter, there's very seldom any need to use 'new' and 'delete'
> directly in your code, unless you are implementing a special dynamic data
> container (and even that happens quite rarely in practice, as it's not
> very common to need a data container that's different from what the
> standard library offers).
>
> The lesson is: Only use 'new' and 'delete' if you absolutely must. Before
> resorting to them, see if the standard containers will do what you want
> in the way you want. It will not only be safer and less error-prone, it
> will also be much simpler and your code will become clearer and easier
> to read.


As does Item 16 in Scott Meyer's Effective C++. Brilliant book.
--
Tactical Nuclear Kittens

 
Reply With Quote
 
 
 
 
Øyvind Røtvold
Guest
Posts: n/a
 
      10-07-2012


(E-Mail Removed) writes:

[ ... ]

You should trim the quoting in your replies, also it appears that your
newsreader puts double linefeeds in your quoting.

> Hi Thanks for the reply. Yes, giving different names solves the
> problem in the production code. However, the problem does not occur
> when you have only this much in a program(i.e on a sample
> program). My program has thousands of lines of code and I am not
> able to post all those lines due to legal aspects with my employer.


OK, you should have indicated that this was just an example in your
original post.

> That is why my suspicion is somehow the compiler has this same name
> bug in certain not so simple cases. Thanks so much.


The usual advice would be to simplify the original program until you
have something that can be posted, or until the bug disappears, in
your case this appears to be achieved, but I doubt that you've found
the real bug.

Sometimes one comes across a class of bugs where seemingly unrelated
or meaningless changes causes the bug to disappear. Often the bug
isn't at that place at all, rather there's some spurious overwrites
elsewhere in your program, the use of memset with constant numbers as
size may be an indication of a programming style where these kind of
errors would be likely.

Changing other parts of the code may cause code or data to move around
thus making the overwrites hit some other part of your data and cause
some other bug that you may or may not see as a failure in your tests.

I suggest you use a memory access checker such as purify - or the
system that I believe exist within Visual Studio to check for
overwrites.

--
... Øyvind - soon to appear in a kill file near you.
... Ignorance can be cured; stupidity is forever.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ajax call to fetch multiple results from multiple queries and showing them 1 by 1 as the results comes. Biranchi Narayan Panda ASP .Net 0 02-21-2010 03:46 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
Cisco IP phones deregistering/registering sporadically (STP switch problem).... wr Cisco 1 05-24-2004 04:36 AM
Response.Redirect Crashes IE 6 sporadically. Cheung, Jeffrey Jing-Yen ASP .Net 3 08-19-2003 04:15 PM



Advertisments