-
March 16th, 2010, 02:36 PM
#1
Passing By Reference then Deleting the Passed Object
Why does the following code not break after delete bar? It seems like the object foo would have a reference to a deleted object and crash in printBar(). Is bar not really gone after the "delete bar" line?
Code:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Bar
{
public:
Bar() {}
void print() {
cerr << "Bar::print(), Hey there!!!!" << endl;
}
private:
};
class Foo
{
public:
Foo(Bar& b) : myBar(b) {}
void printBar() {
myBar.print();
}
private:
Bar& myBar;
};
int main(int argc, char *argv[])
{
Bar* bar = new Bar();
Foo* foo = new Foo(*bar);
delete bar;
bar->print();
for (int i = 0; i < 10; ++i) {
foo->printBar();
}
system("PAUSE");
return EXIT_SUCCESS;
}
I tried searching but all search results turn up more general information about references.
Thanks.
-
March 16th, 2010, 02:53 PM
#2
Re: Passing By Reference then Deleting the Passed Object
Because this is undefined behavior. It might work, it might not. It might cause a bug to manifest itself several thousand lines of code later. You never know.
Incidentally, your Bar :: print function doesn't really do anything with the object itself, so this would work too:
Code:
Bar *pBar = NULL;
pBar->print();
Viggy
-
March 16th, 2010, 02:56 PM
#3
Re: Passing By Reference then Deleting the Passed Object
You are experiencing what is called a dangling pointer/reference which results in undefined behavior. The moment that you delete the bar object all pointers to that object are pointing to memory that was returned to the heap. Since you make the call on the dangling pointer right after the delete it may seem that the code works fine and produces reasonable results. On some systems you might see the program crash. The behavior is undefined so there is no way to predict what will happen. It is up to you the programmer to prevent this undefined behavior.
-
March 16th, 2010, 02:56 PM
#4
Re: Passing By Reference then Deleting the Passed Object
Originally Posted by MrViggy
Incidentally, your Bar :: print function doesn't really do anything with the object itself, so this would work too:
Code:
Bar *pBar = NULL;
pBar->print();
Viggy
On most compilers, yes, that would work, since you're never touching the (invalid) this pointer. It should not be taken to mean that it *must* work, of course.
-
March 16th, 2010, 03:19 PM
#5
Re: Passing By Reference then Deleting the Passed Object
Originally Posted by Lindley
On most compilers, yes, that would work, since you're never touching the (invalid) this pointer. It should not be taken to mean that it *must* work, of course.
Agreed. I didn't mean to imply that it must work for all cases. Sorry about that!
Viggy
-
March 16th, 2010, 11:07 PM
#6
Re: Passing By Reference then Deleting the Passed Object
Code with undefined behaviour is neither working nor non-working. I would rather describe it as dead man walking.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|