Re: Reusing a deleted pointer.
From: Alan (alan_at_surfbest.net)
Date: 12/20/04
- Next message: Tom Widmer: "Re: what is the C++ Standard and compiler for Windows XP Users?"
- Previous message: Rafal 'Raf256' Maj: "Re: read/write stream - parsing"
- In reply to: Alan: "Re: Reusing a deleted pointer."
- Next in thread: Tom Widmer: "Re: Reusing a deleted pointer."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 20 Dec 2004 07:38:15 -0500
"Alan" <alan@surfbest.net> wrote in message news:10sdfhcan0ti0b0@news.supernews.com...
>
> "Dave Townsend" <datownsend@comcast.net> wrote in message news:M4udnWPENK2NG1ncRVn-oA@comcast.com...
> >
> > "Alan" <alan@surfbest.net> wrote in message
> > news:10s8qs23g9vbd17@news.supernews.com...
> [snip]
> > To follow up on the other replies, if you look more carefully at the
> > contents of the object "b" you
> > will see ( at least on VC++6) that after deletion, the memory used by b is
> > written over and the original
> > value a is destroyed! I tinkered a bit with this program, if you do a few
> > more allocations of A's, you
> > will eventually find that an object is allocated at the same address as b,
> > very nasty, now modifying b modifies
> > some other unrelated object! . Just goes to show, just because you don't
> > crash, all may not be well.
> >
> > By the way, your object is too simple to cause a crash, the original
> > address of b would almost always point to to valid memory so deferencing it
> > would not cause a crash after
> > you delete it - just put some virtual functions in your class and try
> > calling one of those after deletion and then
> > you'll crash for sure..!
> >
> > dave
>
> I've put a virtual function in but it didn't fail until I introduced a static
> variable into the class for instance counting. I've cut and cut the
> original program.. The code below (I'm using VC++6 too) succeeds
> or fails, per user choice. What puzzles me is why one class fails and
> the other doesn't - as the differences appear small.
>
> #include <iostream>
> using namespace std;
>
> class Failure {
> int a;
> static int count; // count instances, display them
> public:
> Failure(): a(0) { cout << "constructor " << count++ << endl; }
> ~Failure() { cout << "destructor " << --count << endl; }
> virtual void set(const int& aa) { a=aa; }
> };
>
> class Success {
> int a;
> static int count; // count instances, no display
> public:
> Success(): a(0) { count++; cout << "constructor ?" << endl; }
> ~Success() { --count; cout << "destructor ?" << endl; }
> virtual void set(const int& aa) { a=aa; }
> };
>
> int Failure::count = 1;
> int Success::count = 1;
>
> int main(int argc) {
>
> // no command line arguments (succeeds)
> if( argc == 1 )
>
> Success* s = new Success;
> delete s;
> cout << "class Success *s: try { s->set(1000) }" << endl;
> try {
> s->set(1000);
> } catch(...) { cout << "try failed" << endl; exit(-1); }
> }
>
> // one or more command line arguments (fails)
> else {
> Failure* f = new Failure;
> delete f;
> cout << "class Failure *f: try { f->set(1000) }" << endl;
> try {
> f->set(1000);
> } catch(...) { cout << "try failed" << endl; exit(-1); }
> }
>
> cout << "No failure" << endl;
> return 0;
> }
>
> Regards,
> Alan
There is an opening brace missing after "if( argc == 1 )",
should read "if(argc == 1) {"
I can't explain it - it is there in the original file ;-)
- Next message: Tom Widmer: "Re: what is the C++ Standard and compiler for Windows XP Users?"
- Previous message: Rafal 'Raf256' Maj: "Re: read/write stream - parsing"
- In reply to: Alan: "Re: Reusing a deleted pointer."
- Next in thread: Tom Widmer: "Re: Reusing a deleted pointer."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|