PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Alignment of variables in structs
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Alignment of variables in structs

Réponse
 
LinkBack Outils de la discussion
Vieux 29/11/2007, 18h41   #1
Rick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Alignment of variables in structs

Given:

struct x
{
int i;
char c;
}st_x;

int size_of_struct = sizeof( struct x );
int size_of_st_x = sizeof( st_x );

struct x st_array[ 5 ];

int size_of_st_array = sizeof( st_array );

If an int is 32 bits and a char is 8 bits, are there any guarantees as
to what size_of_struct, size_of_st_x, or size_of_st_array evaluate to?
(they could be 5, 5, and 25, or 8, 8, and 40.)

Are there any rules that specify whether compilers are required to
align variables on any specific boundaries?

I tried all of this on gcc on cygwin on Windows XP and got
size_of_struct = 8, size_of_st_x = 8, and size_of_st_array = 40,
rather than what one might expect at first glance, size_of_struct = 5,
size_of_st_x = 5, and size_of_st_array = 25. Am I guaranteed to get
that on all platforms?

If there are any such guarantees, do any of you have a reference to a
document that says so?

Thanks...
  Réponse avec citation
Vieux 29/11/2007, 18h58   #2
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Alignment of variables in structs

Rick wrote On 11/29/07 13:41,:
> Given:
>
> struct x
> {
> int i;
> char c;
> }st_x;
>
> int size_of_struct = sizeof( struct x );
> int size_of_st_x = sizeof( st_x );
>
> struct x st_array[ 5 ];
>
> int size_of_st_array = sizeof( st_array );
>
> If an int is 32 bits and a char is 8 bits, are there any guarantees as
> to what size_of_struct, size_of_st_x, or size_of_st_array evaluate to?
> (they could be 5, 5, and 25, or 8, 8, and 40.)


sizeof(struct x) == sizeof st_x, always.

sizeof st_array == 5 * sizeof(struct x), always.

sizeof(struct x) >= sizeof(int) + sizeof(char), always.

> Are there any rules that specify whether compilers are required to
> align variables on any specific boundaries?


The implementation is allowed but not required to have
alignment restrictions. The only "requirement" is the
vacuous one that a `char' has no alignment requirement (it
can reside at any address that is a multiple of 1).

> I tried all of this on gcc on cygwin on Windows XP and got
> size_of_struct = 8, size_of_st_x = 8, and size_of_st_array = 40,
> rather than what one might expect at first glance, size_of_struct = 5,
> size_of_st_x = 5, and size_of_st_array = 25. Am I guaranteed to get
> that on all platforms?


No, although it's a common outcome. Presumably, the
compiler is trying to align each `int' to a four-byte
boundary. To make sure that both st_array[0].i and
st_array[1].i are properly aligned, the compiler must
ensure that sizeof(struct x) is a multiple of four, which
it does by inserting three unused "padding" bytes.

> If there are any such guarantees, do any of you have a reference to a
> document that says so?


There are no such guarantees -- which probably means
the Internet has *lots* of documents saying there are ;-)

--
Eric.Sosman@sun.com

  Réponse avec citation
Vieux 29/11/2007, 21h01   #3
jameskuyper@verizon.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Alignment of variables in structs

Rick wrote:
> Given:
>
> struct x
> {
> int i;
> char c;
> }st_x;
>
> int size_of_struct = sizeof( struct x );
> int size_of_st_x = sizeof( st_x );
>
> struct x st_array[ 5 ];
>
> int size_of_st_array = sizeof( st_array );
>
> If an int is 32 bits and a char is 8 bits, are there any guarantees as
> to what size_of_struct, size_of_st_x, or size_of_st_array evaluate to?
> (they could be 5, 5, and 25, or 8, 8, and 40.)


There are some guarantees, just not as many as you thought:
size_of_struct >= sizeof(int)+sizeof(char)
size_of_struct == size_of_st_x
size_of_st_array == 5*size_of_struct
  Réponse avec citation
Vieux 30/11/2007, 02h44   #4
SM Ryan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Alignment of variables in structs

Rick <reply.in.newsgroup@spam.no> wrote:

# Are there any rules that specify whether compilers are required to
# align variables on any specific boundaries?

The struct is large enough to hold all fields, and every
field without a bit width is addressable (you can do
&(s.f)). Beyond that, ANSI C doesn't give you a lot of
guarentees.

Individual compilers may give you tighter guarentees
but then you're tied to those compilers.

You pays your money and you takes your chances.

# I tried all of this on gcc on cygwin on Windows XP and got
# size_of_struct = 8, size_of_st_x = 8, and size_of_st_array = 40,
# rather than what one might expect at first glance, size_of_struct = 5,
# size_of_st_x = 5, and size_of_st_array = 25. Am I guaranteed to get
# that on all platforms?

You're guarenteed sizeof(struct st_x)>=sizeof(int)+sizeof(char)
by ANSI C. gcc has options to pack with no alignment spacing; that
will get you a size of 5, even if it slows down the loads and stores.
However that is gcc specific.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10679 seconds with 12 queries