memory alignment of structs...

R

.rhavin grobert

let say i have

struct SA {
long l1;
long l2:
long l3;
};

struct SB: public SA {
long l4;
};

SB test;

- - - - - -

is is save to do the following...

void* pl3 = ((long*) &test) + 2
void* pl4 = ((long*) &test) + 3

.... to get pointers to l3 and l4?

i'm asking because it works (on my visual c compiled code at least)
but i wanna know
if i can always guess that the above structures will be in memory this
way:

[ long l1 ][ long l2 ][ long l3 ][ long l4 ]


is this any behaviour that is defined in c++ or is it system-
dependent?

thx for any thoughts, -.rhavin;)
 
V

Victor Bazarov

..rhavin grobert said:
let say i have

struct SA {
long l1;
long l2:
long l3;
};

struct SB: public SA {
long l4;
};

SB test;

- - - - - -

is is save to do the following...

void* pl3 = ((long*) &test) + 2
void* pl4 = ((long*) &test) + 3

... to get pointers to l3 and l4?

i'm asking because it works (on my visual c compiled code at least)
but i wanna know
if i can always guess that the above structures will be in memory this
way:

[ long l1 ][ long l2 ][ long l3 ][ long l4 ]


is this any behaviour that is defined in c++ or is it system-
dependent?

The layout, IIRC, is only guaranteed for POD types. Since your 'SB'
has a base class, it's not a POD class, which means the layout is
not guaranteed to be the same as you described for every compiler
out there.

To guarantee the layout you could define 'SB' as

struct SB {
SA base;
long l4;
};

which makes it POD. Of course, 'SB' wouldn't have members named
'l1', 'l2', and 'l3', but instead you'd need to use 'base.l1' and
so on.

V
 
O

Old Wolf

The layout, IIRC, is only guaranteed for POD types.

The POD type guarantees that the members are in that order, but
there could still be any amount of padding between them.
 
J

James Kanze

.rhavin grobert said:
let say i have
struct SA {
long l1;
long l2:
long l3;
};
struct SB: public SA {
long l4;
};
SB test;
- - - - - -
is is save to do the following...
void* pl3 = ((long*) &test) + 2
void* pl4 = ((long*) &test) + 3
... to get pointers to l3 and l4?
i'm asking because it works (on my visual c compiled code at
least) but i wanna know if i can always guess that the above
structures will be in memory this way:
[ long l1 ][ long l2 ][ long l3 ][ long l4 ]
is this any behaviour that is defined in c++ or is it system-
dependent?
The layout, IIRC, is only guaranteed for POD types.

There are some guarantees even for non-POD types, and no total
guarantees even for POD types. In his case, all he's guaranteed
is that l3 comes after l2 comes after l1. l4 could come
anywhere, and there could be any amount of padding, anywhere.
If he has a variable of type SA (a PODS), he is also guaranteed
that the address of l1 is also the address of the variable, but
that is really the only additional guarantee he has; there can
still be any amount of padding anywhere but at the start.

In practice, of course, I can't conceive of an implementation in
which there would be any padding in the above, so about the only
uncertainly in practice is whether l4 comes before or after the
rest. But that's in practice---the standard doesn't guarantee
it.
Since your 'SB'
has a base class, it's not a POD class, which means the layout is
not guaranteed to be the same as you described for every compiler
out there.
To guarantee the layout you could define 'SB' as
struct SB {
SA base;
long l4;
};
which makes it POD. Of course, 'SB' wouldn't have members named
'l1', 'l2', and 'l3', but instead you'd need to use 'base.l1' and
so on.

You still aren't guarantee that there won't be some additional
padding.
 

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,780
Messages
2,569,608
Members
45,247
Latest member
crypto tax software1

Latest Threads

Top