new variable cause crash

S

sdgallardo

HI All,

I have a question
I have a class X and Y
which is working fine, but when I tried to add a new field to class Y
before the obj c
i causes my app to crash, but if I place the new field after the obj c
its ok.
any ideas why this happens/

class Y{

int a;
int b;
obj c;

}

class X : public Y
{

}

X::X() :Y()
{

}
 
V

Victor Bazarov

I have a question
I have a class X and Y
which is working fine, but when I tried to add a new field to class Y
before the obj c
i causes my app to crash, but if I place the new field after the obj c
its ok.
any ideas why this happens/

It could be because somebody is trying to find the address of 'c' by
adding 2*sizeof(int) (or worse, a hard-coded number) to the address of
a 'Y'. As soon as you stick something there, it's not 'c' any more
that occupies that spot.

That's, of course, just a wild guess. To learn how to get the best
answer your problem deserves, read FAQ 5.8.
class Y{

int a;
int b;
obj c;

}

class X : public Y
{

}

X::X() :Y()
{

}

V
 
P

Peter Jansson

HI All,

I have a question
I have a class X and Y
which is working fine, but when I tried to add a new field to class Y
before the obj c
i causes my app to crash, but if I place the new field after the obj c
its ok.
any ideas why this happens/

class Y{

int a;
int b;
obj c;

}

class X : public Y
{

}

X::X() :Y()
{

}

Please provide all of your code (I guess you have removed major portions
of your code).

Sincerely,
Peter Jansson
 
H

Howard

HI All,

I have a question
I have a class X and Y
which is working fine, but when I tried to add a new field to class Y
before the obj c
i causes my app to crash, but if I place the new field after the obj c
its ok.
any ideas why this happens/

class Y{

int a;
int b;
obj c;

what's an "obj"?

semi-colon required here
class X : public Y
{

}

semi-colon required here
X::X() :Y()
{

}

Did you leave out the declaration of this constructor in the class
definition of X above? Same with Y::Y(). (I'll assume you just didn't post
them here, for brevity I guess.)

As shown, this shouldn't compile, because of the missing semi-colons. Also,
"obj" has to be defined somewhere. Apparently, you've left out too much
vital information.

Anyway, check the definition of "obj", and its constructor. Also, try using
your debugger and step through the program to see where it crashes and why.

If that doesn't help, post minimal, compileable code that demonstrates the
problem.

-Howard
 
J

Jim Langston

HI All,

I have a question
I have a class X and Y
which is working fine, but when I tried to add a new field to class Y
before the obj c
i causes my app to crash, but if I place the new field after the obj c
its ok.
any ideas why this happens/

class Y{

int a;
int b;
obj c;

}

class X : public Y
{

}

X::X() :Y()
{

}

Most likely a memory overflow problem. Array bounds and such. This
produces undefined behavior, and it changes a lot when the order variables
and placed in memory is changed. Check all your array bounds, especially in
obj c. You are probably overflowing there.
 
D

Default User

HI All,

I have a question
I have a class X and Y
which is working fine, but when I tried to add a new field to class Y
before the obj c
i causes my app to crash, but if I place the new field after the obj c
its ok.
any ideas why this happens/

Please provide a complete, minimal program that demonstrates the
problem. When your car breaks down, do you bring the mechanic an
artist's sketch of the parts you think are the problem?



Brian
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top