473,770 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_t his_acts_bug_fr ee {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;

struct MyFinalStruct_t his_produces_th e_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;

int main() {
printf("...test ing the first structure:\n");
MyFinalStruct_t his_acts_bug_fr ee Test1;
printf("...test ing the second structure:\n");
MyFinalStruct_t his_produces_th e_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 2055
fa************@ REMOVETHISTOMAI LMEomega64.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************@ REMOVETHISTOMAI LMEomega64.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>@MyFinalStr uct_this_produc es_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************ @REMOVETHISTOMA ILMEomega64.com wrote in
message
news:45******** *************** @reader1.news.t in.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_t his_acts_bug_fr ee {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;

struct MyFinalStruct_t his_produces_th e_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;

int main() {
printf("...test ing the first structure:\n");
MyFinalStruct_t his_acts_bug_fr ee Test1;
printf("...test ing the second structure:\n");
MyFinalStruct_t his_produces_th e_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_t his_produces_th e_bug nested structs
everything compiled and ran properly:

struct MyFinalStruct_t his_produces_th e_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.no wrote:
>* 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**********@g mail.comwrote:
>I get this:

playground.o bj : fatal error LNK1179: invalid or corrupt file:
duplicate COMDAT
'??0<unnamed-tag>@MyFinalStr uct_this_produc es_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>@MyFinalStr uct_this_produc es_the_bug@@QAE @X). The
MyFinalStruct_t his_produces_th e_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
5407
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 INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Jon Combe' , 1 ) INSERT INTO JCTable ( CustomerName , CustomerNo ) VALUES ( 'Bill Gates' , 1 ) UPDATE JCTable SET CustomerNo = 2 WHERE CustomerName = 'Jon Combe'
10
2065
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 << " and &s = " << &s << "\n";
10
2567
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 forgets to implement some method I normally get a compile error(if the class is created in some way of course). When using the Visual Studio 6 compiler however, it does not generate a compile error in this case: - I have a implementing class A...
52
6824
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 assignment
2
1404
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 symbol inside. move the mouse cursor over it. It say the break won't be hit for no runnable code associated with it. But I can stop at the first position and get to the second one by trace step by step. In fact, the lines below that has the same...
8
1539
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 applications, which runs fine in Debug mode, it is crashing in the destructor of a local object on the stack when it is built in release mode. An example of the C++ that causes the problem is as follows (apologies for the contrived example): -
10
4029
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
0
1077
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 allow me to download an optional export log if need be. The first time around, I click on the export log, everything works like it should. The second time i click on the same button, I receive "Access
126
4427
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 theoretical framework for analyzing C programs. In contrast to other books where the actual technical difficulties are "abstracted away", this books tries to analyze real C programs taking into account pointers, stack frames, etc.
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.