Padding in structs 
Author Message
 Padding in structs

Hi all!!

Usually it can happen that sizeof(struct struct_type) doesn't
yield the expected value because of padding added for memory
alignment.
Is this padding always added at the end of the struct memory
area?

In my code I receive a pointer to a struct field, and from it
I have to obtain the pointer of the struct itself by doing:

struct_ptr = (char *)structfield_ptr - offset;

Is it "offsetof()" macro a 100% portable way to obtain that
"offset"?

Thanks a lot!
/Alvaro



Fri, 16 May 2003 03:00:00 GMT  
 Padding in structs



Quote:
> Hi all!!

> Usually it can happen that sizeof(struct struct_type) doesn't
> yield the expected value because of padding added for memory
> alignment.
> Is this padding always added at the end of the struct memory
> area?

No. Usually the compiler will pack the struct to match the architecture's
natural word boundary. That is, on a 32-bit processor you might expect:

struct someTag {
   char  a;
   long  b;
   char  c;
   short d;

Quote:
};

To be padded in memory like:

a = 1 + 3 pad bytes
b = 4 bytes
c = 1 + 3 pad bytes
d = 2 + 2 pad bytes

so the struct takes up 16 bytes instead of 8.

This is completely implementation defined and C has no control over this. Your
compiler may support #pragma pack(1) /* MSVC */ which will prevent any padding
thus making the expected struct size match the actual size.



Fri, 16 May 2003 03:00:00 GMT  
 Padding in structs
On Mon, 27 Nov 2000 20:41:27 +0100, Alvaro Puente Gonzalez

Quote:

>Hi all!!

>Usually it can happen that sizeof(struct struct_type) doesn't
>yield the expected value because of padding added for memory
>alignment.
>Is this padding always added at the end of the struct memory
>area?

No, it can be added anywhere in the struct except at the beginning.  That is to
say, a pointer to the struct can be converted to a pointer to the first member
and vice versa.  There may be padding between the members or at the end.
This is done for various reasons: in order to achieve necessary alignment,
efficient access or binary compatibility.

Even padding at the end may be needed for alignment; consider that a struct can
be used to derive an array type. The members of element [0] will be aligned;
the padding at the end may be needed so that the members of element [1] and
every subsequent element are aligned also.

Quote:
>In my code I receive a pointer to a struct field, and from it
>I have to obtain the pointer of the struct itself by doing:

>struct_ptr = (char *)structfield_ptr - offset;

>Is it "offsetof()" macro a 100% portable way to obtain that
>"offset"?

The offsetof macro is defined by ANSI C as a component of the <stddef.h>
header, which must be present in every conforming freestanding or hosted
implementation of C. If the <stddef.h> header is missing, or fails to define a
conforming offsetof() macro, then you are looking at a non-conforming language
implementation. So offsetof() is 100% portable to conforming implementations
of C, not 100% portable.


Fri, 16 May 2003 03:00:00 GMT  
 Padding in structs

Quote:

> Usually the compiler will pack the struct to match the architecture's
> natural word boundary. That is, on a 32-bit processor you might expect:

> struct someTag {
>    char  a;
>    long  b;
>    char  c;
>    short d;
> };

> To be padded in memory like:

> a = 1 + 3 pad bytes
> b = 4 bytes
> c = 1 + 3 pad bytes
> d = 2 + 2 pad bytes

> so the struct takes up 16 bytes instead of 8.
> This is completely implementation defined and C has no control over this.

To illustrate this, you might also expect (gcc/Linux/586):
a= 1 + 3 pad bytes
b= 4 bytes
c= 1 + 1 bytes
d= 2 bytes
for a total of 12 bytes, 16-bits values being aligned on even addresses.

Regis.



Sat, 17 May 2003 03:00:00 GMT  
 Padding in structs


Quote:


> > Usually it can happen that sizeof(struct struct_type) doesn't
> > yield the expected value

it doesn't yeild anything I don't expect...

Quote:
> > because of padding added for memory alignment.

<snip>

Quote:
> Your compiler may support #pragma pack(1) /* MSVC */ which will
> prevent any padding thus making the expected struct size match the
> actual size.

yes but don't use this. It is totally unportable.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 17 May 2003 03:00:00 GMT  
 Padding in structs


Quote:
>On Mon, 27 Nov 2000 20:41:27 +0100, Alvaro Puente Gonzalez

>>Hi all!!

>>Usually it can happen that sizeof(struct struct_type) doesn't
>>yield the expected value because of padding added for memory
>>alignment.
>>Is this padding always added at the end of the struct memory
>>area?

>No, it can be added anywhere in the struct except at the beginning.  That is to
>say, a pointer to the struct can be converted to a pointer to the first member
>and vice versa.

I've never been fully convinced of the logic that connects these two
statements.  C90 says, "A pointer to a structure object, suitably
converted, points to its initial member ..., and vice versa.  There may
therefore be unnamed padding within a structure object, but not at its
beginning..."  Since the standard clearly states that there may not be
padding at the beginning of a structure, that must be the case, but the
"therefore" in there is unconvincing.  The requirement of equality of
pointers "suitably converted" means that given the declaration

  struct foo { int a; } x;

(&x == (struct foo *)&xa) and (&x.a == (int *)&x)

but I don't see that this requires that

  (char *)&x == (char *)&x.a or (void *)&x == (void *)&x.a

What am I missing in the connotations of "suitably converted"?

--

Lucent Technologies Software Products Group



Fri, 23 May 2003 03:00:00 GMT  
 Padding in structs

Quote:
>Usually it can happen that sizeof(struct struct_type) doesn't
>yield the expected value because of padding added for memory
>alignment.

This means you need to get your expecter fixed.
Don't expect until you have printed out the value of
sizeof(struct struct_type), and then expect that size
for that particular compiler.

Quote:
>Is this padding always added at the end of the struct memory
>area?

No.  It can be added anywhere but at the beginning.  
Typically it is added before a structure element that
is large and has strict alignment requirements
(e.g. double, long, pointer).

                                Gordon L. Burditt



Fri, 30 May 2003 07:12:48 GMT  
 Padding in structs

Quote:

>>Usually it can happen that sizeof(struct struct_type) doesn't
>>yield the expected value because of padding added for memory
>>alignment.
....
>>Is this padding always added at the end of the struct memory
>>area?

>No.  It can be added anywhere but at the beginning.  
>Typically it is added before a structure element that

... or after of the element.

Quote:
>is large and has strict alignment requirements
>(e.g. double, long, pointer).

Not sure what "large" means here, or why. The requirement is
alignment.

--
Mark McIntyre
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 30 May 2003 07:51:41 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. padding a struct to a defined size

2. padding in structs..

3. padding in structs

4. padding in a statically initialized struct

5. struct padding and sizeof

6. sizeof(struct) and padding

7. Q: howto prevent struct padding

8. create a padded struct?

9. C++ 5.0 and sizeof (struct) / padding ?

10. preventing struct padding

11. Padding to mulitple-of-8-bytes: too much padding added

12. ? pad spare ?

 

 
Powered by phpBB® Forum Software