Size of structures 
Author Message
 Size of structures

This is done on Unix environment.
I have two structures and one is nested in the other structure.
typedef struct
{
  unsigned short one:1;
  unsigned short two:2;

Quote:
} Notes;

The size of this structure is 2 Bytes.
typedef struct
{
  long lnum;
  Notes rec;
  float fnum;
Quote:
}

The size of this structure should be 10 Bytes, but I get 12 Bytes.
There are two extra Bytes.
Does anyone know why and how to fix it?
Thanks for your help.


Mon, 21 Jul 1997 22:35:17 GMT  
 Size of structures

Quote:

> This is done on Unix environment.
> I have two structures and one is nested in the other structure.
> typedef struct
> {
>   unsigned short one:1;
>   unsigned short two:2;
> } Notes;
> The size of this structure is 2 Bytes.
> typedef struct
> {
>   long lnum;
>   Notes rec;
>   float fnum;
> }
> The size of this structure should be 10 Bytes, but I get 12 Bytes.
> There are two extra Bytes.
> Does anyone know why and how to fix it?

Fix what?  Why do you think it should be 10 bytes?

A struct may contain padding to force alignment.  Many systems require
floats be aligned on a 4 byte boundary and would put padding before fnum
or rec to force this.  Some require longs be aligned on a 4 byte boundary and
would put padding at the end to make the struct size a multiple of 4 (this is
needed so array of struct will work properly.

Some compilers provide switches to control struct alignment.  Typically
these are for hardware on which poor alignment carries a relatively small
performance penalty.  This is not required by the standard an is not practical
on all processors.

--
Mike Rubenstein



Tue, 22 Jul 1997 21:25:20 GMT  
 Size of structures

Quote:

>This is done on Unix environment.
>I have two structures and one is nested in the other structure.
>typedef struct
>{
>  unsigned short one:1;
>  unsigned short two:2;
>} Notes;
>The size of this structure is 2 Bytes.
>typedef struct
>{
>  long lnum;
>  Notes rec;
>  float fnum;
>}
>The size of this structure should be 10 Bytes, but I get 12 Bytes.
>There are two extra Bytes.
>Does anyone know why and how to fix it?
>Thanks for your help.

This is an `alignment' problem. Some types required to start in memory at an
address which is a multiple of 4, such as long and float. Your `float fnum'
can't begin immediately after the `Notes' structure, so you lose 2 bytes.
Try to put Notes after float and see the difference. It should give 10 bytes
this time. However, some compilers may have extra constraints due to addressing
and you may still get 12 bytes. Anyway, try it.

--

Services Informatiques, Universite de Montreal.
C.P. 6128, Succursale centre-ville, Montreal,
Quebec, Canada.   H3C 3J7



Wed, 23 Jul 1997 05:26:54 GMT  
 Size of structures

Quote:

>The size of this structure should be 10 Bytes, but I get 12 Bytes.
>There are two extra Bytes.
>Does anyone know why and how to fix it?

This is covered in the FAQ list.  One place you can get it by
ftp is rtfm.mit.edu in the pub/usenet/comp.lang.c/ directory.

--



Wed, 23 Jul 1997 14:21:15 GMT  
 Size of structures

Quote:

>This is done on Unix environment.
>I have two structures and one is nested in the other structure.
>typedef struct
>{
>  unsigned short one:1;
>  unsigned short two:2;
>} Notes;
>The size of this structure is 2 Bytes.
>typedef struct
>{
>  long lnum;
>  Notes rec;
>  float fnum;
>}
>The size of this structure should be 10 Bytes, but I get 12 Bytes.
>There are two extra Bytes.
>Does anyone know why and how to fix it?
>Thanks for your help.

Many machine architectures require that large datatypes (like floating point numbers) start on
specified boundaries.  It looks like your machine requires that floats start on a 32 bit boundary.
To accomplish this in a structure, the compiler puts in padding, so the structure really
contains

long  integer   (32 bits)
short integer   (8 bits)
short integer   (8 bits)
padding          (16 bits)
floating point   (whatever size)

I'd have to check my reference  (Harbison and Steele is a good one for this type of thing), but
I think it would be wrong to assume that the padding space contents are any kind of
rational data.  (i.e.  initialized to zero, copied on structure assignment, ...)
---
Chris Howard
daylight software
http://www.infonet.net/showcase/dssshow



Wed, 23 Jul 1997 22:22:12 GMT  
 Size of structures

Quote:

>>typedef struct
>>{
>>  long lnum;
>>  Notes rec;
>>  float fnum;
>>}
>>The size of this structure should be 10 Bytes, but I get 12 Bytes.
>>There are two extra Bytes.

>This is an `alignment' problem. Some types required to start in memory at an
>address which is a multiple of 4, such as long and float. Your `float fnum'
>can't begin immediately after the `Notes' structure, so you lose 2 bytes.
>Try to put Notes after float and see the difference. It should give 10 bytes
>this time. However, some compilers may have extra constraints due to addressing
>and you may still get 12 bytes. Anyway, try it.

Bogus advice.  Even if all the fields in the new version of the structure
are properly aligned, the compiler has to make sure that an array of such
structures has all the members properly aligned, so the size will be still
12, with the 2 padding bytes moved at the end of the structure (most
likely, because the compiler is free to do anything it likes with
structure padding -- there is only one guarantee: there is no padding
before the first field).

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Fri, 25 Jul 1997 01:00:22 GMT  
 Size of structures

Quote:

> This is done on Unix environment.
> I have two structures and one is nested in the other structure.
> typedef struct
> {
>   unsigned short one:1;
>   unsigned short two:2;
> } Notes;
> The size of this structure is 2 Bytes.
> typedef struct
> {
>   long lnum;
>   Notes rec;
>   float fnum;
> }
> The size of this structure should be 10 Bytes, but I get 12 Bytes.
> There are two extra Bytes.
> Does anyone know why and how to fix it?
> Thanks for your help.

I think there is a rule saying something like
any element must/will be placed on a position that is
a multiplum of its own size. Since fnum uses 4 bytes
it will be placed on an address that is a multiplum of
16 and there will be a two-bytes empty space between
rec and fnum. Try

typedef struct
{
  long lnum;
  float fnum;
  Notes rec;

Quote:
}

Maybe this will help. But those things may architecture-specific
also.

Tord



Fri, 25 Jul 1997 20:17:53 GMT  
 Size of structures

Quote:


> > typedef struct
> > {
> >   unsigned short one:1;
> >   unsigned short two:2;
> > } Notes;
> > The size of this structure is 2 Bytes.
> > typedef struct
> > {
> >   long lnum;
> >   Notes rec;
> >   float fnum;
> > }
> > The size of this structure should be 10 Bytes, but I get 12 Bytes.
> > There are two extra Bytes.
> > Does anyone know why and how to fix it?
> > Thanks for your help.

> I think there is a rule saying something like
> any element must/will be placed on a position that is
> a multiplum of its own size. Since fnum uses 4 bytes
> it will be placed on an address that is a multiplum of
> 16 and there will be a two-bytes empty space between
> rec and fnum.

The rule you have in mind applies to arrays. For instance, if some type
`type' has been defined and an array

  type arr[N];

has been declared, then we know that

  (char *) &arr[i] == (char *) arr + i * sizeof (type)

(assuming that i is in the range 0..N of course.)

There is no such rule though with regard to the offsets of members in a
struct. All that matters here is that the machine's alignment restrictions
are met, and will keep being met when an array of such structs is declared.
To achieve that, padding may be added.

It is nowhere said that "any element must/will be placed on a position that
is a multiplum of its own size." On a machine without alignment restrictions,
a struct member of a two-byte type (e.g. int on certain compilers on certain
8-bit byte machines) may well be placed at an odd address.

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 894099
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

Famous last words: Segmentation Fault (core dumped)



Sat, 02 Aug 1997 20:27:16 GMT  
 Size of structures

Quote:



>>>typedef struct
>>>{
>>>  long lnum;
>>>  Notes rec;
>>>  float fnum;
>>>}
>>>The size of this structure should be 10 Bytes, but I get 12 Bytes.
>>>There are two extra Bytes.

>>This is an `alignment' problem. Some types required to start in memory at an
>>address which is a multiple of 4, such as long and float. Your `float fnum'
>>can't begin immediately after the `Notes' structure, so you lose 2 bytes.
>>Try to put Notes after float and see the difference. It should give 10 bytes
>>this time. However, some compilers may have extra constraints due to addressing
>>and you may still get 12 bytes. Anyway, try it.
>Bogus advice.  Even if all the fields in the new version of the structure
>are properly aligned, the compiler has to make sure that an array of such
>structures has all the members properly aligned, so the size will be still
>12, with the 2 padding bytes moved at the end of the structure (most
>likely, because the compiler is free to do anything it likes with
>structure padding -- there is only one guarantee: there is no padding
>before the first field).

If what is needed is the ability to stop the compiler from aligning
members of structures, check the compiler documentation for some pragma
or command line flag. Most compilers have the ability to do this, but
don't do so by default since modern computer architectures tend to run
more slowly if memory accesses are byte-aligned.
--
=============================================================================

(61 3) 353 2471               | You're looking hale and hearty, little buddy.
Kodak (Australia) Pty Ltd     | I'm a coffee achiever, Sam


Tue, 05 Aug 1997 05:53:03 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. binary file and different size of structures

2. Size of structure containing char fields

3. size of structure

4. Find size of variable size structure?

5. event for Size structure

6. Structures of indefinite size

7. Structure problem - Size not known???

8. how to find the size of a private structure

9. how to copy a structure of record to another structure with type,size in the menber of structure...

10. zero sized structures

11. Help Me !! some strange in a structure size

12. The size of the structure array

 

 
Powered by phpBB® Forum Software