473,320 Members | 2,133 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,320 software developers and data experts.

Initialize reference to itself

I just spent two and a half days chasing down a bug resulting from
initializing a reference to itself, like so:

struct Bar;
struct Foo {
Bar& bar;
Foo() : bar(bar) {}
};

The compiler I was using (gcc 3.2.3) issued no warning for this. I
assume that any use of bar produces undefined behavior. If this is the
case, then it seems to be a defect in the compiler not to issue a
warning. Is my analysis correct? Specifically:

(1) Is there any conceivable circumstance in which this will produce
anything but undefined behavior?
(2) is there any reason why a compiler would have difficulty detecting
this, in the general case?
(3) Anyone happen to know if later versions of gcc, or other compilers,
address this?

Please note that while I'm referring to a specific compiler here, my
question (at least, part (1)) is really about language behavior, hence
not (IMHO) OT.

Luke

Jan 11 '06 #1
8 3104
On 11 Jan 2006 13:58:29 -0800, "Luke Meyers" <n.***********@gmail.com>
wrote:
I just spent two and a half days chasing down a bug resulting from
initializing a reference to itself, like so:

struct Bar;
struct Foo {
Bar& bar;
Foo() : bar(bar) {}
};
It's about time that you got a copy of the C++ standard. <g>
The compiler I was using (gcc 3.2.3) issued no warning for this. I
assume that any use of bar produces undefined behavior. If this is the
case, then it seems to be a defect in the compiler not to issue a
warning. Is my analysis correct? Specifically:

(1) Is there any conceivable circumstance in which this will produce
anything but undefined behavior?
No. See 8.3.2, par. 4. A reference must be initialized with a "valid
object or function". Therefore, a reference cannot be initialized with
itself.
(2) is there any reason why a compiler would have difficulty detecting
this, in the general case?
I'm not sure whether a compiler diagnostic is required here, but it is
UDB in any case. Note that a compiler vendor (or implementation) is
not necessarily required to issue a warning or error when your code
induces UDB.
(3) Anyone happen to know if later versions of gcc, or other compilers,
address this?

Please note that while I'm referring to a specific compiler here, my
question (at least, part (1)) is really about language behavior, hence
not (IMHO) OT.

Luke


--
Bob Hairgrove
No**********@Home.com
Jan 11 '06 #2
Bob Hairgrove wrote:

I'm not sure whether a compiler diagnostic is required here, but it is
UDB in any case. Note that a compiler vendor (or implementation) is
not necessarily required to issue a warning or error when your code
induces UDB.


Undefined behavior arises from invalid code for which a diagnostic is
not required. So, no, a conforming implementation is not required to
diagnose this construct. And with good reason: it's hard to formulate
the true rule in a way that can always be diagnosed. In this case it's
easy. In others it's not:

struct node
{
node *next;
};

node nil = { &nil }; // legal and useful
node nil = get_node(&nil);

node get_node(node *ptr)
{ // okay, as above
node n;
n.next = ptr;
return n;
}

node get_node(node *ptr)
{ // trouble
return *ptr;
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 11 '06 #3
Bob Hairgrove wrote:
On 11 Jan 2006 13:58:29 -0800, "Luke Meyers" <n.***********@gmail.com>
wrote:
I just spent two and a half days chasing down a bug resulting from
initializing a reference to itself, like so:

struct Bar;
struct Foo {
Bar& bar;
Foo() : bar(bar) {}
};


It's about time that you got a copy of the C++ standard. <g>


It's in the mail on its way, don't worry. And the two and a half days
were spent on a chain of inferences and simplification which led me to
actually *look* at the offending line of code, at which time I
immediately realized the problem. The standard wouldn't have helped me
at that point.

The reason the error was introduced originally was that my intent was
to initialize a reference owned by an inner class with a variable of
the same name in its containing class. Dumb mistake, but we all make
those and it's helpful if a compiler can catch the easy ones.
(2) is there any reason why a compiler would have difficulty detecting
this, in the general case?


I'm not sure whether a compiler diagnostic is required here, but it is
UDB in any case. Note that a compiler vendor (or implementation) is
not necessarily required to issue a warning or error when your code
induces UDB.


I'm not surprised that it's not required; my point was that this would
be a useful case for a compiler to issue a non-mandatory warning.

Anyway, thanks.

Luke

Jan 12 '06 #4
What is UDB?

Jan 12 '06 #5

Roman Werpachowski wrote:
What is UDB?


UDB is an acronym for "Un Defined Behaviour"

<joke>
What it means is that compiler is free to mail all private letters on
your harddrive to all people and newsgroups in your address book,
including comp.lang.c++
</joke>

Jan 12 '06 #6
Neelesh Bodas wrote:
Roman Werpachowski wrote:
What is UDB?

UDB is an acronym for "Un Defined Behaviour"

<joke>
What it means is that compiler is free to mail all private letters on
your harddrive to all people and newsgroups in your address book,
including comp.lang.c++
</joke>


Fortunately, not too many compilers do that. Otherwise, the SNR in this
group would be pretty bad.
Jan 12 '06 #7
Neelesh Bodas wrote:
Roman Werpachowski wrote:
What is UDB?

UDB is an acronym for "Un Defined Behaviour"

<joke>
What it means is that compiler is free to mail all private letters on
your harddrive to all people and newsgroups in your address book,
including comp.lang.c++
</joke>


More formally, it means that the C++ Language definition doesn't say
what happens when you use that code. In some cases the result is benign
and predictable; in others it's not.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 12 '06 #8
You could always get a syntax checker such as pc-lint or another
compiler (just to double check the warnings). Another compiler or
syntax checker may catch stuff which your compiler misses.
------------------------------------------------
http:://defendUSA.blogspot.com
www.cafepress.com/bush_doggers

Jan 12 '06 #9

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

Similar topics

74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
4
by: yogesh | last post by:
mysql in c++ initialize error occurs a simple program is executed in redhat9.0 , using gcc 3.2.2 compiler version ... #include <stdio.h> #include <mysql.h> #include <string.h> int main() {
38
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: ...
1
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, I just upgraded the motherboard and cpu on my computer (core2Duo). I also imaged the contents of my old harddrive to a new one(500g) - Windows XP sp2 (version2002). Everything appears...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
4
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.