Struct alignment

H

Hipo

Hi.
Assuming a struct:

struct data{
int, a, b, c, d;
};

Can I assume that the four variables from data are stored in memory in
order given above? Or would it be possible that they look like this in
memory: a, c, d, b?

I'm using the Visual Studio 2005 Pro compiler.

g, Hipo
 
V

Victor Bazarov

Hipo said:
Assuming a struct:

struct data{
int, a, b, c, d;
};

Can I assume that the four variables from data are stored in memory in
order given above?

Yes, that's mandated by the Standard.
Or would it be possible that they look like this in
memory: a, c, d, b?
No.

I'm using the Visual Studio 2005 Pro compiler.

Shouldn't matter.

V
 
P

phlip

Hipo said:
Great, that saves my day :)

You can't assume they aren't padded; that's implementation specific.

(Yes, VC++ probably doesn't pad them.)

Don't rely on it. You weren't going to treat that struct as binary data,
were you?
 
T

Tomás

Hipo posted:
Hi.
Assuming a struct:

struct data{
int, a, b, c, d;
};

Can I assume that the four variables from data are stored in memory in
order given above? Or would it be possible that they look like this in
memory: a, c, d, b?

I'm using the Visual Studio 2005 Pro compiler.

g, Hipo


Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
members -> Array elements".


-Tomás
 
J

Jack Klein

Hipo posted:



Long discussion on comp.std.c++ about this right now. Thread Title: "Struct
members -> Array elements".


-Tomás

No, this question is only about the order, not about the possibility
of padding between the members.
 
T

Tomás

Jack Klein posted:

No, this question is only about the order, not about the possibility
of padding between the members.

I realise that... but the order of the members is of no relevance unless
you try to do something funky like access them with pointers:

data obj;

int *p = &obj.a;

*p = 1; /* We're assuming this is A */

*++p = 2; /* We're assuming this is B */

*++p = 3; /* We're assuming this is C */

*++p = 4; /* We're assuming this is D */


If you don't try something funky like this, then there's no reason to
care about the order of the members.

(Which makes me wonder: Why does C++ even guarantee that the members be
in order one-after-another in a POD struct? We already don't have a
guarantee of the absence of padding... so there's no point in
guaranteeing that they be in order).

Okay... well... there might be ONE use:

bool IsAhead( const int * const a, const int * const b )
{
return b > a;
}

int main()
{
data obj;

IsAhead( &obj.a, &obj.b );
}


-Tomás
 

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

Similar Threads

union/struct alignment safety 2
struct alignment 14
Blue J Ciphertext Program 2
Data alignment questin, structures 46
alignment issues 8
safe "struct hack"? 11
gcc alignment options 19
Alignment problems 20

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top