473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Dangling Reference

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
Jul 19 '05 #1
4 5808

"kaede" <ka************@hotmail.com> wrote in message news:60**************************@posting.google.c om...

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.
Jul 19 '05 #2
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"kaede" <ka************@hotmail.com> wrote in message news:60**************************@posting.google.c om...

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
Jul 19 '05 #3
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"kaede" <ka************@hotmail.com> wrote in message news:60**************************@posting.google.c om...

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
Jul 19 '05 #4
kaede wrote:


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.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Aravind | last post by:
I would like to know in what manner dangling pointers affect the security of a application developed using C++.What are the loopholes that are created by dangling pointers and how they could be...
11
by: John | last post by:
Hi: Below is a simple code: class link1 { public: link1(); link1(int &b1, double &b2); int* a1;
1
by: Nick Keighley | last post by:
I saw this in some code I'm maintaining. Is it a bad idea? class T { public #: T () i_mem(0) { } T (T2 &t2) i_mem(0), t2_ref (t2)
6
by: Tony Johansson | last post by:
Hello Experts! I'm reading in a book about C++ and the book says common errors is "A function should not return a constant references to its parameter passed by constant references" Why? Here...
6
by: Matthias Kaeppler | last post by:
Hi, I have a question regarding references, and their chance to "dangle" (if that's possible at all): Say I have a collection ob objects, and I take a reference to one of them. Now I sort...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
10
by: Belebele | last post by:
Suppose that I have a method that returns references to "elements" in an iterator, and a method to advance the iterator: class Element { /* ... */ }; class Iterator { public: Element&...
1
by: sridhard2406 | last post by:
Hi All, I have a doubt on undrestanding Dangling pointers.Below I mentioned sample code. please let me know, my view on Dangling pointers is correct or not? main( ) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.