Referencing destroyed objects

C

Ciccio

Hi all,

In one of my little programming efforts, I wrote a piece of
code that creates references to destroyed objects. The output
and the piece of code that creates it can be found below this
post.

I have two questions related to this program and its output.
1) Why is the copy constructor not called in the first part?
2) Why is, in the second part, the referenced object destroyed
before the object carrying the reference. And why is this not
the case in the first part.
2b) Is this valid c++ ???

I have tested this on various g++ compilers and also the intel
compilers. All give the same output. Valgrind only detects an
error when I try to access broken data.

Thanks for the help,
Klaas

The output and the code are given here
// OUTPUT
inside bar_fun()
construct foo: 0x7fff1bce5508
construct bar: 0x7fff1bce5508
return foo pointer: 0x7fff1bce5508

inside bar_fun()
construct foo: 0x7fff1bce5518
construct bar: 0x7fff1bce5518
construct foo: 0x7fff1bce5528
mirror bar: 0x7fff1bce5528
destruct bar: 0x7fff1bce5518
destruct foo: 0x7fff1bce5518
return foo pointer: 0x7fff1bce5518
destruct bar: 0x7fff1bce5528
destruct foo: 0x7fff1bce5528
destruct bar: 0x7fff1bce5508
destruct foo: 0x7fff1bce5508

// CODE
#include <iostream>

struct foo {
foo() { std::cout << "construct foo: " << this << std::endl;
}
~foo() { std::cout << "destruct foo: " << this << std::endl;
}
void ret_ptr() { std::cout << "return foo pointer: " << this
<< std::endl; }
char c;
};

struct bar {
bar(): f(f_) { std::cout << "construct bar: " << this <<
std::endl; }
bar(bar &b): f(f_) { std::cout << "not copy constructor bar:
" << this << std::endl; }
bar(const bar &b): f(f_) { std::cout << "copy constructor
bar: " << this << std::endl; }
bar(const bar &b, int i): f(b.f) { std::cout << "mirror bar:
" << this << std::endl; }
~bar() { std::cout << "destruct bar: " << this << std::endl;
}

foo f_;
foo &f;
};

bar bar_fun() {
std::cout << "inside bar_fun()" << std::endl;
return bar();
}

int main() {
bar b1(bar_fun());
b1.f.ret_ptr();
std::cout << std::endl;
bar b2(bar_fun(),1);
b2.f.ret_ptr();
};
 
V

Victor Bazarov

Ciccio said:
Hi all,

In one of my little programming efforts, I wrote a piece of
code that creates references to destroyed objects. The output
and the piece of code that creates it can be found below this
post.

I have two questions related to this program and its output.
1) Why is the copy constructor not called in the first part?

It's optimized away, most likely. The Standard allows it.
2) Why is, in the second part, the referenced object destroyed
before the object carrying the reference. And why is this not
the case in the first part.

By "referenced" you mean 'temp_bar.f_'? It's a subobject of a
temporary. The temporaries are destroyed as soon as then can be. If
you try retaining a reference to some part of the temporary that is
about to be destroyed, your reference (which you have retained) is
simply invalid, no other rules apply.

Here is the common confusion is about the references and temporaries: if
I retain a reference to a temporary, the temporary is going to survive
as long as the reference *no matter how I obtained the reference*.
That's simply not true (the emphasized part). The lifetime of a
temporary is extended only as long as the *very first reference* that is
initialized with it.

Example 1:

bar const& bcref = bar_fun(); // temporary is created, returned and
// starts its prolonged life - as long
// as the lifetime of 'bcref'.

Example 2:

bar const& pass_ref_along(bar const& another)
{
return another; // return value initialized from another ref
}

bar const& bcref = pass_ref_along( bar_fun() ); // temporary is
// created and destroyed as soon as 'pass_ref_along' returns
// because that's the lifetime of the *argument* reference
// which cannot be extended - there is no "ownership of the
// referred temporary" that one reference can pass to another
// reference somehow
2b) Is this valid c++ ???

No. You initialize the reference member 'f' of 'b2' object from what is
a temporary object (returned from 'bar_fun()'). As soon as the c-tor
finishes executing, your 'f' member is invalid. Any use of it has
undefined behaviour.
I have tested this on various g++ compilers and also the intel
compilers. All give the same output. Valgrind only detects an
error when I try to access broken data.

That's what "use" is for references. If you try passing the invalid 'f'
member to a function that expects a const ref 'valgrind' will probably
flag that too, but maybe not, who knows? The reference is invalid
nonetheless, as soon as the referenced object (temp_bar.f_) is destroyed
along with its containing object (temp_bar). ***


*** 'temp_bar' designates the temporary bar object returned from a call
to 'bar_fun'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top