This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: 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.





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]