473,396 Members | 1,683 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,396 software developers and data experts.

Very weird compiler bug or normal ISO C++ behaviour?


Hello all,
I went across what seems possibly a bug of the compiler (VisualC 2005,
just for the record) or a very strange and non-expected (by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.

Here we go: I noticed that if I nest a structure inside another one,
the wrong constructor will be called.. the one from a different class!!

Here is some test code:

---

struct One {
int o;
One() {
printf("Into the constructor of One which is inherited by ");
};
};

struct Two : One {
int t;
Two() {
printf("Two\n");
};
};

struct Zwei : One {
int z;
Zwei() {
printf("Zwei\n");
};
};

struct Dos : One {
int d;
Dos() {
printf("Dos\n");
};
};

struct MyFinalStruct_this_acts_bug_free {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;

struct MyFinalStruct_this_produces_the_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;

int main() {
printf("...testing the first structure:\n");
MyFinalStruct_this_acts_bug_free Test1;
printf("...testing the second structure:\n");
MyFinalStruct_this_produces_the_bug Test2;
// --
return(0);
}

---

The above program produces this output, at least on my compiler:

---

....testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
....testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two

---

Is this normal? The first output is what I would definitely expect
also from the other structure!

Thanks,
Fabio

Sep 11 '06 #1
9 2016
fa************@REMOVETHISTOMAILMEomega64.com wrote:
Hello all,
I went across what seems possibly a bug of the compiler (VisualC 2005,
just for the record) or a very strange and non-expected (by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.
[snip code]
Is this normal? The first output is what I would definitely expect
also from the other structure!
It appears to be a bug. After adding the needed

"#include <cstdio>
using namespace std"

, GCC produces the following output:

[ccox-macbook:~] ccox% g++ test.cpp && ./a.out
....testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
....testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos

--
Clark S. Cox III
cl*******@gmail.com
Sep 11 '06 #2
fa************@REMOVETHISTOMAILMEomega64.com wrote:
I went across what seems possibly a bug of the compiler (VisualC 2005,
just for the record) or a very strange and non-expected (by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.

Here we go: I noticed that if I nest a structure inside another one,
the wrong constructor will be called.. the one from a different class!!
[code]

FWIW, GCC 4.1 disagrees with MSC14 in that aspect, it produces the same
output for both cases as one would expect.

Uli

Sep 11 '06 #3
I get this:

playground.obj : fatal error LNK1179: invalid or corrupt file:
duplicate COMDAT
'??0<unnamed-tag>@MyFinalStruct_this_produces_the_bug@@QAE@XZ'

This is also VC++ 2005. Standard edition. This is a copy paste of the
OP code with namespace std and some includes.

Yeah, obviously buggy. I did a google search for this error and came
up with a number of things, all unrelated to each other and none fit
this situation.

Fun stuff.

Sep 11 '06 #4
<fa************@REMOVETHISTOMAILMEomega64.comwro te in
message
news:45***********************@reader1.news.tin.it ...
>
Hello all,
I went across what seems possibly a bug of the compiler
(VisualC 2005,
just for the record) or a very strange and non-expected
(by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on
both newsgroups.

Here we go: I noticed that if I nest a structure inside
another one,
the wrong constructor will be called.. the one from a
different class!!

Here is some test code:

---

struct One {
int o;
One() {
printf("Into the constructor of One which is
inherited by ");
};
};

struct Two : One {
int t;
Two() {
printf("Two\n");
};
};

struct Zwei : One {
int z;
Zwei() {
printf("Zwei\n");
};
};

struct Dos : One {
int d;
Dos() {
printf("Dos\n");
};
};

struct MyFinalStruct_this_acts_bug_free {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;

struct MyFinalStruct_this_produces_the_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;

int main() {
printf("...testing the first structure:\n");
MyFinalStruct_this_acts_bug_free Test1;
printf("...testing the second structure:\n");
MyFinalStruct_this_produces_the_bug Test2;
// --
return(0);
}

---

The above program produces this output, at least on my
compiler:

---

...testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
...testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two

---

Is this normal? The first output is what I would
definitely expect
also from the other structure!

I get the same linking error as Noah Roberts. Actually,
anonymous structures are not allowed by the C++ Standard,
however MSVC++ permits them as MS extension. When I added
names to MyFinalStruct_this_produces_the_bug nested structs
everything compiled and ran properly:

struct MyFinalStruct_this_produces_the_bug {
struct tagA {
Two MyObject1;
} A;
struct tagB {
Zwei MyObject2;
} B;
struct tagC {
Dos MyObject3;
} C;
} ;
Sep 11 '06 #5
* Alex Blekhman:
anonymous structures are not allowed by the C++ Standard,
Aren't they?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 11 '06 #6
Alf P. Steinbach <al***@start.nowrote:
>* Alex Blekhman:
>anonymous structures are not allowed by the C++ Standard,
>Aren't they?
This would seem a good thing to clear up. ;) Seems you
need them for C back-compatibility.

Steve
Sep 11 '06 #7
"Alf P. Steinbach" wrote:
>anonymous structures are not allowed by the C++ Standard,

Aren't they?

Hm.. It appears that you're right. I confused OP example
with declaration of unnamed anonymous struct:

// compiles with MS extensions enabled, fails with /Za
switch
struct A
{
struct
{
int i;
};
};
Sep 11 '06 #8
Alex Blekhman wrote:
I get the same linking error as Noah Roberts. Actually,
anonymous structures are not allowed by the C++ Standard,
There is no such thing as an anonymous struct (in the sense
of an anonymous union) in C++. These are not anonymous structs
in that sense, they are just unnamed ones. Since the struct
does define a variable, it is not anonymous and legal in this
use.
Sep 12 '06 #9
"Noah Roberts" <ro**********@gmail.comwrote:
>I get this:

playground.obj : fatal error LNK1179: invalid or corrupt file:
duplicate COMDAT
'??0<unnamed-tag>@MyFinalStruct_this_produces_the_bug@@QAE@XZ'

This is also VC++ 2005. Standard edition. This is a copy paste of the
OP code with namespace std and some includes.

Yeah, obviously buggy.
I also get the linker error, with both VC++ 2003 and VC++ 2005, but if you
look at the generated code with /Fc, you can see that it generates three
separate constructors, but gives them all the exact same name
(??0<unnamed-tag>@MyFinalStruct_this_produces_the_bug@@QAE@X). The
MyFinalStruct_this_produces_the_bug constructor then calls this name three
times.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 13 '06 #10

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

Similar topics

7
by: Jon Combe | last post by:
I have created the following test SQL code to illustrate a real problem I have with some SQL code. CREATE TABLE JCTable ( CustomerName varchar(50) ) ALTER TABLE JCTable ADD CustomerNo int...
10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
52
by: entropy123 | last post by:
Hey all, I'm working with some legacy C code and I would like to compile it as a CPP file. I get the following error message: driver.cpp:87: cannot convert `void *' to `GenericStruct *' in...
2
by: Fu Chen | last post by:
Hi! I have really weird break point. Look at my screen shoot http://www.mapsea.com/vs.jpg First break is normal and can stop when program run to that position. Second break show with a question...
8
by: Daniel Yelland | last post by:
Hi, I have developed a number of code libraries in Win32 DLLs and have written a number of test suite executables that implicitly link to these libraries in order to test them. In one of my test...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
0
by: probir.chatterjee | last post by:
Hi, Has anyone encountered similar problem before. I have an webpage in a webapplication that usesDataSet .WriteXml method to write a Log file. The page is meant to export out files in excel and...
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.