This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
RE: A C++ question regarding the delete operator
- From: "Chris Sekula" <chriss at turnpikeglobal dot com>
- To: "Rosimildo da Silva" <rosimildo at hotmail dot com>
- Cc: <ecos-discuss at sources dot redhat dot com>
- Date: Wed, 2 Jan 2002 09:07:09 -0500
- Subject: RE: [ECOS] A C++ question regarding the delete operator
Thanks for your response. Your interpretation of my question (an
automatic variable on the stack) was correct.
CHRIS.
-----Original Message-----
From: Rosimildo da Silva
Sent: Tue 1/1/2002 11:57 AM
To: Chris Sekula
Cc: ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] A C++ question regarding the delete operator
From: "Chris Sekula" <chriss@turnpikeglobal.com>
To: "Rosimildo da Silva" <rosimildo@hotmail.com>
Cc: <ecos-discuss@sources.redhat.com>
Sent: Tuesday, January 01, 2002 10:02 AM
Subject: RE: [ECOS] A C++ question regarding the delete operator
> Yes, after looking through the code a bit more thoroughly, I
believe
> that memory had been allocated in another method when the
pointer was
> created.
OK.
> However, I'm still curious as to what may happen if 'delete'
is called
> on a pointer that is created locally without the 'new'
operator ever
> being invoked.
I do not understand what you mean by "created locally", if you
mean
an automatic variable on stack, such as:
void foo()
{
MyClass x;
// some code
MyClass *px = &x;
// more stuff
// THIS PROBABLY CAUSES A CRASH, OR EVEN DESTROY
// THE HEAP. WORST, THE PROGRAM MOST LIKELY WILL
// NOT CRASH NOW, BUT LATER WHEN ANOTHER POINT
// TRY TO ACCESS THE HEAP OR MEMORY POOLS, ETC.
delete px;
}
Botton line: Only use "delete" for things allocated with "new"
new ---------> free ==> BAD, VERY BAD
malloc-------> delete ==> BAD, VERY BAD
Do not read below:
-------------------
There is a way to "initialize" an object at a given "buffer". It
is called
"placement new", but you do not want deal with this unless you
have
at least 5 years of C++ ( doing it everyday !!! <g> ).
Rosimildo.