Topic: What does the delete operator delete?
Author: Eduard Witteveen <E.Y.Witteveen@research.kpn.com>
Date: 1999/05/07 Raw View
Alex Vinokur wrote in message <7gmfl4$r6m$1@nnrp1.dejanews.com>...
>
>Hi,
>
>What does the delete operator delete?
>How to explain the results of this program?
The delete operator tells the OS that the part where ur variable was now may
be used by other things. It does not delete anything, but more frees the
reference of ur program to it. When u keep a pointer to that part of memory,
and try to read it, the result is unpredictable(altrough in most cases u get
the same value that u placed in it).
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/05/06 Raw View
In article <7gmfl4$r6m$1@nnrp1.dejanews.com>, Alex Vinokur <alexander.vi
nokur@telrad.co.il> writes
>Hi,
>
>What does the delete operator delete?
>How to explain the results of this program?
>
> Thanks in advance,
> Alex
If you insist on calling delete on the address of an auto variable the
program can do anything it likes including destroying your computer. If
you insist you may call a destructor on an auto object, but you better
make sure you provide a correct replacement before you reach the end of
its scope, but you do not own the memory in which an auto object is
constructed so you better not try to give it back.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Dimitar Atanassov" <Dimitar.Atanassov@waii.com>
Date: 1999/05/06 Raw View
The delete operator marks the respective memory block as free so that the
system can allocate
that memory location should a request come.
But delete does not clear that memory location. It only calls the
destructor of the class and marks the memory location as free.
what happens is that the memory is marked as free, and the data is still
there and your variable still points to
that memory location.
Therefore you can do what you are doing, but you are not guaranteed that the
data will be there, since that is marked as
free memory and can be allocated at any time.
Strongly suggest to avoid this situation.
Dimitar Atanassov
Dimitar.Atanassov@bakeratlas.com
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Ali Cehreli" <ac10@Berkeley.Net.com>
Date: 1999/05/06 Raw View
Alex Vinokur <alexander.vinokur@telrad.co.il> wrote in article
<7gmfl4$r6m$1@nnrp1.dejanews.com>...
>
> Hi,
>
> What does the delete operator delete?
> How to explain the results of this program?
>
> Thanks in advance,
> Alex
[...]
> int main()
> {
> //==========================
> AAA iAAA (100);
>
> iAAA.show ();
> delete &iAAA;
Only objects created by new should be deleted. delete frees the memory
allocated by new.
[...]
>
> //#########################################################
> //------------------- Running Results : BEGIN -------------
> void AAA::show(); value_ = 100
> AAA::~AAA(); value_ = 101
> void AAA::show(); value_ = 101 // ??? After delete &iAAA
>
> void AAA::show(); value_ = 200
> AAA::~AAA(); value_ = 201
> void AAA::show(); value_ = 201 // ??? After delete pAAA
> AAA::~AAA(); value_ = 102 // ??? After delete &iAAA
>
> //------------------- Running Results : END ---------------
Were you running the release version of the program? My MSVC++5 gave the
same results as yours in the release build. But the debug version failed a
debug assertion on the "delete &iAAA" statement as expected.
[...]
Ali
--
Please remove the dot between Berkeley and Net
to get my correct e-mail address.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/05/04 Raw View
Hi,
What does the delete operator delete?
How to explain the results of this program?
Thanks in advance,
Alex
//#########################################################
//------------------- C++ code : BEGIN -------------------
#include <iostream.h>
class AAA
{
private :
int value_;
public :
AAA (int value_i = 15)
{
value_ = value_i;
}
~AAA ()
{
value_++;
cout << __PRETTY_FUNCTION__
<< "; value_ = "
<< value_
<< endl;
}
void show ()
{
cout << __PRETTY_FUNCTION__
<< "; value_ = "
<< value_
<< endl;
}
};
int main()
{
//==========================
AAA iAAA (100);
iAAA.show ();
delete &iAAA;
iAAA.show ();
//=====================
cout << endl;
AAA* pAAA = new AAA(200);
pAAA->show ();
delete pAAA;
pAAA->show ();
return 0;
}
//------------------- C++ code : END ----------------------
//#########################################################
//------------------- Running Results : BEGIN -------------
void AAA::show(); value_ = 100
AAA::~AAA(); value_ = 101
void AAA::show(); value_ = 101 // ??? After delete &iAAA
void AAA::show(); value_ = 200
AAA::~AAA(); value_ = 201
void AAA::show(); value_ = 201 // ??? After delete pAAA
AAA::~AAA(); value_ = 102 // ??? After delete &iAAA
//------------------- Running Results : END ---------------
//#########################################################
//------------------- Compiler & System ------------------
g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)
uname -a : SunOS <nodename> 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5
//---------------------------------------------------------
//#########################################################
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: dcarapic@barok.foi.hr (Dalibor Carapic)
Date: 1999/05/05 Raw View
On 4 May 1999 21:18:53 GMT, Alex Vinokur <alexander.vinokur@telrad.co.il>
wrote in <7gmfl4$r6m$1@nnrp1.dejanews.com>:
>
>Hi,
>
>What does the delete operator delete?
>How to explain the results of this program?
>
> Thanks in advance,
> Alex
>
>
It's just a guess but I would say that it simply removes a portion
of memory from a linked list of pointers to allocated memory which
are somewhere in the system. (It doesn't do wipe).
--
_______________________________________________________________________
Dalibor Carapic - Homepage: http://www.foi.hr/~dcarapic
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]