C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dangling Reference

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
kaede
Guest





PostPosted: Mon Oct 27, 2003 5:32 pm    Post subject: Dangling Reference Reply with quote



Hi all,

I have a quick question regarding binding temporary to const
reference. I wrote a little toy program to test it, but I don't seems
to understand the behaviour. Consider the following code snipplet:

// Data class, also overload operator<<
class Data { ... };

// Holds the data
class DataHolder
{
public:
DataHolder(Data data_): m_data(data_) {} // 1
void dump() const { cout << m_data << "n"; }

private:
const Data& m_data;
};

Scenerio A)

int main()
{
DataHolder holder( Data(....) );

// this line will dump out some garbage. My understanding is that
// The member variable m_data binds to a temporary copied of Data
// and when the DataHolder constructor completes, the temporary is
// destroyed that, the m_data reference is no longer valid
holder.dump();

return 1;
}

Scenerio B)
int main()
{
Data data( .. );
DataHolder holder( data );

// this line successfully dump the contents of data. I am not sure
why?
// shouldn't it behaves the same Scenerio A?
holder.dump();
return 1;
}

Scenerio C)
Change the DataHolder constructor to the following

DataHolder(Data& data_): m_data(data_) {} or
DataHolder(const Data& data_): m_data(data_) {}

int main()
{
Data data( .. );
DataHolder holder( data );

// this line successfully dump the contents of data. I am not sure
why?
holder.dump();
return 1;
}

Scenerio D)
Now, rather than using Data as a class, we change data to the
following:

enum Data
{
LONG,
FLOAT,
INT
};

When re-running scenerio A - C, the contents of data seems to be
dumping out
correctly. I am not sure why?

Can anyone enlighten me? Your help is appreciated. Thanks!!!!

Kaede
Back to top
Ron Natalie
Guest





PostPosted: Mon Oct 27, 2003 5:45 pm    Post subject: Re: Dangling Reference Reply with quote




"kaede" <kaedeRukawa_jp (AT) hotmail (DOT) com> wrote


Quote:

When re-running scenerio A - C, the contents of data seems to be
dumping out
correctly. I am not sure why?

Because one of the insidious features of undefined behavior is that things

might appear to work normally (only to fail later).

In Scenario A, you have it right. The temporary ceases to be as soon
as the constructor finishes.

In Scenario B, you have undefined behavior. What happens if you
change the member of data after holder is constructed but before
you print? I suspect either that you are benefiting from the eliding
of the temporary or somehow the reference ends up seated at the
variable in main. In either case it's just a conincidence.

Scenario C, you don't have undefined behavior. There is no temporary.
The reference in holder refers to the variable data in main. It lives longer
than holder does in fact.

In Scenario D, you just get more coincidental behavior, but you've changed
the data object's size so that it's really small and may end up being parked
in a register.



Back to top
kaede
Guest





PostPosted: Mon Oct 27, 2003 10:53 pm    Post subject: Re: Dangling Reference Reply with quote



"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:
"kaede" <kaedeRukawa_jp (AT) hotmail (DOT) com> wrote



When re-running scenerio A - C, the contents of data seems to be
dumping out
correctly. I am not sure why?

Because one of the insidious features of undefined behavior is that things
might appear to work normally (only to fail later).

In Scenario A, you have it right. The temporary ceases to be as soon
as the constructor finishes.

In Scenario B, you have undefined behavior. What happens if you
change the member of data after holder is constructed but before
you print? I suspect either that you are benefiting from the eliding
of the temporary or somehow the reference ends up seated at the
variable in main. In either case it's just a conincidence.

Scenario C, you don't have undefined behavior. There is no temporary.
The reference in holder refers to the variable data in main. It lives longer
than holder does in fact.

In Scenario D, you just get more coincidental behavior, but you've changed
the data object's size so that it's really small and may end up being parked
in a register.

Thanks ... I think I see what is happening.

Regards,
Kaede

Back to top
kaede
Guest





PostPosted: Tue Oct 28, 2003 9:51 pm    Post subject: Re: Dangling Reference Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:
"kaede" <kaedeRukawa_jp (AT) hotmail (DOT) com> wrote



When re-running scenerio A - C, the contents of data seems to be
dumping out
correctly. I am not sure why?

Because one of the insidious features of undefined behavior is that things
might appear to work normally (only to fail later).

In Scenario A, you have it right. The temporary ceases to be as soon
as the constructor finishes.

In Scenario B, you have undefined behavior. What happens if you
change the member of data after holder is constructed but before
you print? I suspect either that you are benefiting from the eliding
of the temporary or somehow the reference ends up seated at the
variable in main. In either case it's just a conincidence.

Scenario C, you don't have undefined behavior. There is no temporary.
The reference in holder refers to the variable data in main. It lives longer
than holder does in fact.

In Scenario D, you just get more coincidental behavior, but you've changed
the data object's size so that it's really small and may end up being parked
in a register.

I was thinking about it this morning and what would happened if I
modified Scenerio C to the following:

int main()
{
DataHolder* p = 0;

{
Data data( ... );

p = new DataHolder(data);

} // (2)

// would this cause undefined behaviour since data already get
destroyed
// after (2). or will the const& m_data prolongs the scope of
data?
p->getType();
}

Similarily, what if it is in different translational unit:

// a.cxx
void A::registerData(Data& data_)
{
DataHolder* p = new DataHolder(data_);

C::registerDataHolder(p);
}

// b.cxx
void B::someFunc()
{
Data data(..);

A.registerData(data);
}

Also, I read something about when temporary binds to const&, the
const& that binds to the temporary will prolongs the life of the
temporary. That is the
temporary will be destroyed when the const& is destoyed. Is that
correct, I don't think I understand the meaning and the usefulless of
it. Can anyone explain it to me. Thanks =)

Kaede

Back to top
lilburne
Guest





PostPosted: Tue Oct 28, 2003 9:52 pm    Post subject: Re: Dangling Reference Reply with quote

kaede wrote:

Quote:


I was thinking about it this morning and what would happened if I
modified Scenerio C to the following:


In both cases you'll have undefined behaviour.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.