sizeof (struct) ! = sizeof struct elements 
Author Message
 sizeof (struct) ! = sizeof struct elements

Hi.
I 've a structure

struct tmp {
        long l;
        short s;
        char abc[3];
        char xyz [10];

Quote:
}

I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
these struct elements in diff. order but i always get 2 different sizes.
infact, even when i remove char arrays, somehow short seems to be of same size as long...
ie 4bytes.. Is there any way of enforcing this. I am using gcc on Linux.

TIA
amol



Mon, 22 Sep 2003 02:03:15 GMT  
 sizeof (struct) ! = sizeof struct elements

Quote:
>Hi.
>I 've a structure

>struct tmp {
>    long l;
>    short s;
>    char abc[3];
>    char xyz [10];
>}

>I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
>equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
>these struct elements in diff. order but i always get 2 different sizes.
>infact, even when i remove char arrays, somehow short seems to be of same size as long...
>ie 4bytes.. Is there any way of enforcing this. I am using gcc on Linux.

I'll paraphrase a bit, but the standard says that the compiler can
align the members of a structure in any fashion sensible to the
compile unit, and that invisible padding will be used to perform this
alignment magic.

Unless your compiler supports a #pragma or compiler option to alter
the padding, you're stuck with what gcc gives you.

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employer's.)



Mon, 22 Sep 2003 02:19:54 GMT  
 sizeof (struct) ! = sizeof struct elements

Quote:

> Hi.
> I 've a structure

> struct tmp {
>         long l;
>         short s;
>         char abc[3];
>         char xyz [10];
> }

> I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
> equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
> these struct elements in diff. order but i always get 2 different sizes.
> infact, even when i remove char arrays, somehow short seems to be of same size as long...
> ie 4bytes.. Is there any way of enforcing this. I am using gcc on Linux.

> TIA
> amol

<OT>
compaq cc has the following option

  -[no]member_alignment
      Directs the compiler to byte-align data structure members (with
the
      exception of bit-field members).

      By default, data structure members are aligned on natural
boundaries
      (that is, on the next boundary appropriate to the type of the
member)
      instead of the next byte. For example, an int variable member is
      aligned on the next longword boundary, and a short variable member
is
      aligned on the next word boundary.

maybe your compiler has something similar.
Anyways, I think it is poor design to rely on how your compiler arranges
members in a struct. Did you consider using offsetof ?
HTH, tobias.
<\OT>



Mon, 22 Sep 2003 02:28:24 GMT  
 sizeof (struct) ! = sizeof struct elements

Quote:
> Hi.
> I 've a structure
> struct tmp {
>    long l;
>    short s;
>    char abc[3];
>    char xyz [10];
> }
> I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
> equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
> these struct elements in diff. order but i always get 2 different sizes.
> infact, even when i remove char arrays, somehow short seems to be of same size as long...
> ie 4bytes.. Is there any way of enforcing this. I am using gcc on Linux.

The C standard says the compiler is free to insert any padding it wants
to either between any two members, or after the last member. It is not
allowed to insert any padding before the first member.
So in answer to your question, there is no way to enforce non-padding.
One trick to guarantee that only the needed memory is allocated is to
abandon structs and keep all members in separate variables, however this
can be very clumsy.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
   - Mika P. Nieminen



Mon, 22 Sep 2003 02:23:05 GMT  
 sizeof (struct) ! = sizeof struct elements

Quote:

> Hi.
> I 've a structure

> struct tmp {
>   long l;
>   short s;
>   char abc[3];
>   char xyz [10];
> }

> I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
> equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
> these struct elements in diff. order but i always get 2 different sizes.
> infact, even when i remove char arrays, somehow short seems to be of same
> size as long... ie 4bytes.. Is there any way of enforcing this. I am using
> gcc on Linux.

    Amout the only way to do this would be to do something like this:

#define PS_L(ptr)   (*(long*)(ptr))
#define PS_S(ptr)   (*(short*)((char*)ptr) + sizeof(long))
#define PS_ABC(ptr) (((char*)ptr) + sizeof(short) + sizeof(long))
#define PS_XYZ(ptr) (((char*)ptr) + sizeof(short) + sizeof(long) + 3)

char *psuedo_struct = malloc( sizeof(long) + sizeof(short) + 13 );
/* Check for NULL here*/

PS_L(psuedo_struct) = ...;
PS_S(psuedo_struct) = ...;
PS_ABC(psuedo_struct)[...]  = ...;
PS_XYZ(psuedo_struct)[...]  = ...;

    However, this will not work on systems that require any type of
special alignment, and may slow down systems that don't.

--
http://www.whereismyhead.com/clark/
Clark S. Cox, III



Mon, 22 Sep 2003 02:47:09 GMT  
 sizeof (struct) ! = sizeof struct elements

Quote:

> Hi.
> I 've a structure

> struct tmp {
>         long l;
>         short s;
>         char abc[3];
>         char xyz [10];
> }

> I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
> equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
> these struct elements in diff. order but i always get 2 different sizes.
> infact, even when i remove char arrays, somehow short seems to be of same size as long...
> ie 4bytes.. Is there any way of enforcing this. I am using gcc on Linux.

The struct must be padding so that an array of struct tmp will be
correctly aligned.  Since sizeof(long) + sizeof(short) + 13 is an
odd number unless sizeof(long) or sizeof(short) is odd, if your
implementation requires any of longs or shorts or chars to be aligned
on an even boundary, there must be at least one byte of padding.


Mon, 22 Sep 2003 05:06:40 GMT  
 sizeof (struct) ! = sizeof struct elements

Quote:
>struct tmp {
>    long l;
>    short s;
>    char abc[3];
>    char xyz [10];
>}

>I want this structure to be tightly packed ie- sizeof(stuct tmp ) should
>equal sizeof(long)+sizeof(short)+ 13 * sizeof(char). I 've tried placing
>these struct elements in diff. order but i always get 2 different sizes.
>infact, even when i remove char arrays, somehow short seems to be of same size as long...
>ie 4bytes.. Is there any way of enforcing this.

Not in standard C.

Quote:
>I am using gcc on Linux.

Then, you may want to repost your question to gnu.gcc.help.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 22 Sep 2003 06:03:47 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

2. VC++ 7.0 sizeof struct issue with 1 byte packing

3. Problem with sizeof of a struct void pointer

4. is sizeof(struct) equal to size of components ??

5. sizeof struct member

6. sizeof a union that includes a struct

7. sizeof struct member outside function?

8. sizeof(short) in struct

9. sizeof(struct)

10. struct padding and sizeof

11. sizeof arrays in structs

12. sizeof(struct)

 

 
Powered by phpBB® Forum Software