Class Member Getting Corrupted

P

Paul Jackson

I have the following situation:

class Y;
class Z;

class X {
public:
Y y;
Z* z;
};

X::X() : y(3) {
z = new Z;
};

class Z is fairly complicated, i.e., it publicly inherits from two base
classes, one of those base classes itself publicly inherits from two base
classes, etc.

When X::X is in the assignment phase, mmebers in y are getting corrupted
during the construction of z.

Are there any typical problems to look for to solve this mess? Using g++,
are there any compiler flags that should be turned on that may point to the
problem area or assist with debugging using gdb?
 
P

Phlip

Paul said:
I have the following situation:

class Y;
class Z;

class X {
public:
Y y;
Z* z;
};

X::X() : y(3) {
z = new Z;
};

class Z is fairly complicated, i.e., it publicly inherits from two base
classes, one of those base classes itself publicly inherits from two base
classes, etc.

When X::X is in the assignment phase, mmebers in y are getting corrupted
during the construction of z.

Are there any typical problems to look for to solve this mess? Using g++,
are there any compiler flags that should be turned on that may point to the
problem area or assist with debugging using gdb?

Chop Z in half (that means comment out half its members, then force it to at
least compile if not behave). The problem might change, or go away.

When the problem goes away, you have chopped the problem out of Z. Now
activate all the code you commented out, and vice versa. Keep chopping
(essentially a binary search algorithm) until you have only a few variables
left.

Then write lots of automated tests on your code so this kind of thing likely
won't happen again.
 
V

Victor Bazarov

Paul Jackson said:
Original post corrected below.



Forward-declarations?

Y is undefined, the compiler is unlikely to let you do that.

Extraneous semicolon...

So, the code you posted is not going to compile. What do you
really expect from us? A recommendation on how to fix the code
that you haven't even posted?

What's in 'etc.'? Post it all, otherwise why post at all?

Memory overrun, most likely. Read the FAQ 5.8.

Here is something that might be similar (the code has undefined
behaviour, but typically memory overrun occurs and the 'y' member
is corrupted):

#include <stdlib.h>

class A {
char x[7];
int y;
public:
A(const char* str) : y(3) {
strcpy(x, str); // undefined behaviour if 'strlen(str)'
// is greater than 6
}
};

int main() {
A a("abcdefghij");
}

As you can see there are only 7 chars allocated for 'x' and 10
characters (plus the null terminator) are used to initialise it.

'strcpy' copies "hij\0" into the member 'y' (most likely). Of
course, it's not defined by the language, but is one of the expected
results.
Using g++,

Particular compiler flags or particular techniques using particular
products should be discussed in their respective newsgroups. Try
gnu.g++.help.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top