variable placement in structures

O

omnia neo

Hi All .. I am facing a rather strange issue.
I have a struct A which is part of struct B as follows:

struct A{
int a;
int b;
....
}

struct B{
struct A a;
int x;
int y;
....
}

so whenever i am trying to add a variable in struct A i get strnage
behaviour in my application and it crashes.

Now when I add the same variable in struct B directly, my application
works just fine!

I have observed this strange behaviour before and only remedy I have
is the placement of variables. Can somebody explain me what is going
on.
 
P

Paul Bibbings

omnia neo said:
Hi All .. I am facing a rather strange issue.
I have a struct A which is part of struct B as follows:

struct A{
int a;
int b;
...
}

struct B{
struct A a;
int x;
int y;
...
}

so whenever i am trying to add a variable in struct A i get strnage
behaviour in my application and it crashes.

Now when I add the same variable in struct B directly, my application
works just fine!

I have observed this strange behaviour before and only remedy I have
is the placement of variables. Can somebody explain me what is going
on.

What does "trying to add a variable in struct A" mean? What exactly is
the "str[an]ge behaviour in [your] application"? How does it crash?
What does it mean to "add the same variable in struct B directly"?

Please provide *complete*, compilable example code that exhibits the
problems that you are wanting us to help you with (as per the FAQ).

Regards

Paul Bibbings
 
O

omnia neo

omnia neo said:
Hi All .. I am facing a rather strange issue.
I have a struct A which is part of struct B as follows:
struct A{
int a;
int b;
...
}
struct B{
struct A a;
int x;
int y;
...
}
so whenever i am trying to add a variable in struct A i get strnage
behaviour in my application and it crashes.
Now when I add the same variable in struct B directly, my application
works just fine!
I have observed this strange behaviour before and only remedy I have
is the placement of variables. Can somebody explain me what is going
on.

What does "trying to add a variable in struct A" mean?  What exactly is
the "str[an]ge behaviour in [your] application"?  How does it crash?
What does it mean to "add the same variable in struct B directly"?

Please provide *complete*, compilable example code that exhibits the
problems that you are wanting us to help you with (as per the FAQ).

Regards

Paul Bibbings- Hide quoted text -

- Show quoted text -

Hello Paul,
Thanks for a quick reply.
Well struct A is instantiated in struct B, so any variable 'var' added
in struct A is part of struct B too i.e. I can acess it via B.A.var.
Hence I used 'directly'. I directly add 'var' variablke in struct B,
even now it is part of strcut B and I can access it via B.var
Does it have to do anything with struct padding or packing ?
 
A

Alf P. Steinbach

* omnia neo, on 14.06.2010 13:15:
omnia neo said:
Hi All .. I am facing a rather strange issue.
I have a struct A which is part of struct B as follows:
struct A{
int a;
int b;
...
}
struct B{
struct A a;
int x;
int y;
...
}
so whenever i am trying to add a variable in struct A i get strnage
behaviour in my application and it crashes.
Now when I add the same variable in struct B directly, my application
works just fine!
I have observed this strange behaviour before and only remedy I have
is the placement of variables. Can somebody explain me what is going
on.

What does "trying to add a variable in struct A" mean? What exactly is
the "str[an]ge behaviour in [your] application"? How does it crash?
What does it mean to "add the same variable in struct B directly"?

Please provide *complete*, compilable example code that exhibits the
problems that you are wanting us to help you with (as per the FAQ).

Regards

Paul Bibbings- Hide quoted text -

- Show quoted text -

Hello Paul,
Thanks for a quick reply.
Well struct A is instantiated in struct B, so any variable 'var' added
in struct A is part of struct B too i.e. I can acess it via B.A.var.
Hence I used 'directly'. I directly add 'var' variablke in struct B,
even now it is part of strcut B and I can access it via B.var
Does it have to do anything with struct padding or packing ?

Your question does not make sense. We can guess, wildly, what your code might be
and what problem it you're encountering. But better: follow Paul's advice.

Locate the FAQ.

Read it.


Cheers & hth.,

- Alf
 
V

Vladimir Jovic

Yannick Tremblay wrote:

[snip]
Things can become even more fun if you have pointers and char in your
struct. E.g

struct A
{
int a;
int b;
int * p;
};

struct B
{
A a;
int x;
int y;
};

In this case, a compilation unit trying to acces *(B.a.p) might be
trying to dereference the value set for (B.a) (i.e. an int) by a
different compilation unit which will likely result in a memory
violation and a crash.

Why it would crash in this case?
B.a->p or *(B.a.p) should work, no?
 
P

Paul Bibbings

omnia neo said:
omnia neo said:
Hi All .. I am facing a rather strange issue.
I have a struct A which is part of struct B as follows:
struct A{
int a;
int b;
...
}
struct B{
struct A a;
int x;
int y;
...
}
so whenever i am trying to add a variable in struct A i get strnage
behaviour in my application and it crashes.
Now when I add the same variable in struct B directly, my application
works just fine!
I have observed this strange behaviour before and only remedy I have
is the placement of variables. Can somebody explain me what is going
on.

What does "trying to add a variable in struct A" mean?  What exactly is
the "str[an]ge behaviour in [your] application"?  How does it crash?
What does it mean to "add the same variable in struct B directly"?

Please provide *complete*, compilable example code that exhibits the
problems that you are wanting us to help you with (as per the FAQ).

Regards

Paul Bibbings- Hide quoted text -

- Show quoted text -

Hello Paul,
Thanks for a quick reply.
Well struct A is instantiated in struct B, so any variable 'var' added
in struct A is part of struct B too i.e. I can acess it via B.A.var.
Hence I used 'directly'. I directly add 'var' variablke in struct B,
even now it is part of strcut B and I can access it via B.var
Does it have to do anything with struct padding or packing ?

Still need that compilable code that illustrates the problem. Your
program is crashing but we cannot *see* it crashing without you giving
us some example code that we can 'crash' for ourselves.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Regards

Paul Bibbings
 
A

AnonMail2005

Hi All .. I am facing a rather strange issue.
I have a struct A which is part of struct B as follows:

struct A{
int a;
int b;
...

}

struct B{
struct A a;
int x;
int y;
...

}

so whenever i am trying to add a variable in struct A i get strnage
behaviour in my application and it crashes.

Now when I add the same variable in struct B directly, my application
works just fine!

I have observed this strange behaviour before and only remedy I have
is the placement of variables. Can somebody explain me what is going
on.

Sounds like you need to recompile *all* of your code so it picks up
the new definition of your struct. Try that first.

HTH
 
O

omnia neo

Thanks for the reply Yannick and all others..

well as was asked above here are some more claritis.
@Paul - I would have loved to put all compilable code but some code
cannot be just put on forums specially if it is some company's
proprietory.
@Anonmail - well I compiled all the code and also deleted the objects.

-I have both my structures in same header file so compilation unit
remains same.
-since both my structures are pretty complex (each one including
several different structures), it is difficult to know where the
arrays are used unappropriately.

For now I solved the issue this way(i still dont know the problem):
I replaced the placement of structure A in B as folows:
previous:
struct A
{
<other structures..its a huge list>
}

strcut B
{
struct A;
<other structures .. its a huge list again>
}


modified to:
struct A
{
<other structures..its a huge list>
}

strcut B
{
<other structures .. its a huge list again>
struct A;
}

Its more of a hack .. so im still looking for a permenant solution.

Yannick Tremblay wrote:
Things can become even more fun if you have pointers and char in your
struct.  E.g
struct A
{
  int a;
  int b;
  int * p;
};
struct B
{
  A a;
  int x;
  int y;
};
In this case, a compilation unit trying to acces *(B.a.p) might be
trying to dereference the value set for (B.a) (i.e. an int) by a
different compilation unit which will likely result in a memory
violation and a crash.
Why it would crash in this case?
B.a->p or *(B.a.p) should work, no?

Typo above, should have been:
"a compilation unit trying to acces *(B.a.p) might be trying to dereference
the value set for (B.x)"
Sorry.

So first compilation unit think the struct is:
struct B
{
  int a.a;
  int a.b;
  int x;
  int y;

}

So typically, the first compilation unit, will assign something like
42 to B.x (at address <base+8>)

The second compilation unit think the struct is:

struct B
{
  int a.a;
  int a.b;
  int * a.p;
  int x;
  int y;

}

So it will try to dereference *(B.a.p) which as it has been compiled
is at address <base+8>, unfortunately, this was set by the different
compilation unit that had been compiled with the older version of
struct B and has assigned an integer to it since it knows it as B.x
and has no knowledge of the existence of B.a.p.

So typically, *(42) tend to result in a crash.

Hope that's clearer.

Yannick- Hide quoted text -

- Show quoted text -
 
P

Paul Bibbings

omnia neo said:
Thanks for the reply Yannick and all others..

well as was asked above here are some more claritis.
@Paul - I would have loved to put all compilable code but some code
cannot be just put on forums specially if it is some company's
proprietory.
@Anonmail - well I compiled all the code and also deleted the objects.

-I have both my structures in same header file so compilation unit
remains same.
-since both my structures are pretty complex (each one including
several different structures), it is difficult to know where the
arrays are used unappropriately.

For now I solved the issue this way(i still dont know the problem):
I replaced the placement of structure A in B as folows:
previous:
struct A
{
<other structures..its a huge list>
}

strcut B
{
struct A;
<other structures .. its a huge list again>
}


modified to:
struct A
{
<other structures..its a huge list>
}

strcut B
{
<other structures .. its a huge list again>
struct A;
}

With respect, I think you have serious problems in relation to your
understanding of your own code (or lack of it) and this is reflected in
your difficulty to communicate that here. For my part, I still do not
have the slightest sense of the aspects of it that you have tried to
convey so far. You will need to be more *precise*.

For instance, in the above pseudo-code your `struct A' within the
definition of B (in whatever order) is a *delaration* of a *type*, yet
you introduced your problem in terms such as "whenever I am trying to
add a *variable*..." Which is it? Are you having trouble with the
placing of local type declarations/definitions, data member positioning,
which?

Be sure of one thing. To say on the one hand "I still don't know the
problem" and then to suppose on the other that "I *solved* the issue
this way" is almost certainly a fallacy. C++ is not a `hold-my-hand'
language. It is possible in a myriad ways to write code that looks like
it `works', but unless you understand fully what that code achieves (and
how it achieves it) then the scope for merely seeming correctness is
vast. In short, drop the idea that you have *solved* anything. To have
`solved' something is to be able to say "I know this works, and I know
*how* this works."

You will need to present some *real* code here. I understand your point
about the need to protect proprietry code, and that is the same for
everyone here. Even where there is not such a requirement, there is
still the further `requirement' that the problem be condensed down to
some minimal example that illustrates the problem, which will mask any
proprietariness in any case.

Put something *else* together. Write a couple of small structs, A and
B, for which you can demonstrate that the placement of your `variables'
or the ordering of your declarations produces an effect the same as,
or similar to, that you are encountering for your codebase. Or, at the
very least, write them (in *actual* code, not pseudo-code) so that we
can get a picture of what you are working with with all the `vast'
aspects taken out, even if you can't recreate the failure. That way we
might be able to say something like "there's a pointer here, and here
you're using an unsafe cast (or something) ... /these/ are the kind of
pitfalls that you might encounter doing /that/." As it stands, I'm not
sure that anyone here has /any/ sense of what it is you are actually
doing.

Be *precise* about your descriptions. Don't just say "I replaced the
placement of structure A in B" because the word structure could be
referring to a declaration/definition of a type, as in:

struct A { };

or an instance of such a type, as in:

A a;

What is it that you have `moved'? Is it "I have reordered the data
members so that the member variable A a is now after..." or is it "I
have moved the declaration/definition of the local struct A to...?"

Also, I might have missed something, but you say "since both my
structures are pretty complex (each one including several different
structures), it is difficult to know where the arrays are used
unappropriately" and I hadn't picked up that you have mentioned *arrays*
previously.

Help us to help you! There are many people here who would welcome the
chance to do just that, given something to get started with.

Good luck.

Regards

Paul Bibbings
 

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
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top