This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++ copy constructor bug


I'm trying to get some code to interact between C and C++.  For some
reason, I was having trouble with my values being completely mangled
on linux (redhat 6.1) -- but not on Solaris 2.6.  I'm using gcc 2.95.2 on
both.  I also get the bug on linux with egcs-2.91.66 (the compiler packaged
with rh6.1).

I've reduced the problem to what's in the attached files.  To get the bug,
leave in the copy constructor for mystruct.  To avoid it, remove the copy
constructor.  The program should print [5,5] twice, but instead prints
garbage if the copy constructor is there.  When debugging, you'll notice
that there's no problem in main; but obviously the wrong values are passed
in to printMyStruct.

I've tried flags -g, -g -O2, and no flags; different garbage is printed
depending on optimization, but it's still garbage.
# 1 "mystruct.c"
 
# 1 "mystruct.h" 1
 



typedef struct mystruct {
    double x,y;




} mystruct;




void printMyStruct(mystruct m);


# 2 "mystruct.c" 2


void printMyStruct(mystruct m) {
    printf("[%g,%g]\n", m.x, m.y);
}
# 1 "driver.cc"
# 1 "mystruct.h" 1
 



typedef struct mystruct {
    double x,y;

    mystruct(const mystruct& other) : x(other.x),y(other.y) { }
    mystruct() { x=y=5; }

} mystruct;


extern "C"

void printMyStruct(mystruct m);


# 1 "driver.cc" 2


int main() {
    mystruct m;
    printMyStruct(m);
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]