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 > Organization of Structs
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Organization of Structs

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 00h45   #1
pradeep
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Organization of Structs

Hello friends,

If we have a struct definition like this

typedef struct s {
char a;
int b;
char c;
} _s;

where because of alignment effects the layout given results in memory
inefficiency, do we need to manually arrange things (char then char
then int) or will a good optimizing compiler automatically reorder the
fields in the struct?

Also if we do arrange it as char then char then int, the first dword
in the struct will have two char bytes and two padding bytes. How will
the compiler organize the space and why? Will it be
padding - padding - char - char
or padding - char - padding - char
or char - char - padding - padding
etc.?

Thanks.
  Réponse avec citation
Vieux 15/04/2008, 01h16   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Organization of Structs

pradeep wrote:
> Hello friends,
>
> If we have a struct definition like this
>
> typedef struct s {
> char a;
> int b;
> char c;
> } _s;
>
> where because of alignment effects the layout given results in memory
> inefficiency, do we need to manually arrange things (char then char
> then int) or will a good optimizing compiler automatically reorder the
> fields in the struct?
>

No, the order is fixed.

> Also if we do arrange it as char then char then int, the first dword
> in the struct will have two char bytes and two padding bytes. How will
> the compiler organize the space and why? Will it be


That is entirely up to the compiler.

--
Ian Collins.
  Réponse avec citation
Vieux 15/04/2008, 01h21   #3
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Organization of Structs

pradeep <nospam@nospam.com> writes:

> If we have a struct definition like this
>
> typedef struct s {
> char a;
> int b;
> char c;
> } _s;
>
> where because of alignment effects the layout given results in memory
> inefficiency, do we need to manually arrange things (char then char
> then int) or will a good optimizing compiler automatically reorder the
> fields in the struct?


The compiler can't re-order the struct.

> Also if we do arrange it as char then char then int, the first dword
> in the struct will have two char bytes and two padding bytes. How will
> the compiler organize the space and why? Will it be
> padding - padding - char - char
> or padding - char - padding - char
> or char - char - padding - padding


The only options are ones that have no padding at the start. That
leaves three possibilities. I'd lay odds on it using char, char,
padding, but you can't assume that it will do so.

As to why, the usually strategy is that the compiler puts a field at
the first offset that will incur no access penalty.

--
Ben.
  Réponse avec citation
Vieux 15/04/2008, 02h33   #4
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Organization of Structs

>If we have a struct definition like this
>
>typedef struct s {
> char a;
> int b;
> char c;
>} _s;
>
>where because of alignment effects the layout given results in memory
>inefficiency, do we need to manually arrange things (char then char
>then int)


It is not necessary to optimize for memory (or for that matter, any
kind of) efficiency until you've proved it's unacceptably bloated.
Most people seem to be worried about SPEED more. If you're worried
about the memory efficiency of this struct, presumably you are
allocating at a minimum tens of thousands of them.

>or will a good optimizing compiler automatically reorder the
>fields in the struct?


No, a C compiler is not allowed to reorder the fields in a struct.

>Also if we do arrange it as char then char then int, the first dword
>in the struct will have two char bytes and two padding bytes. How will
>the compiler organize the space and why? Will it be
>padding - padding - char - char
>or padding - char - padding - char
>or char - char - padding - padding
>etc.?


The compiler can add padding after any field (but not before the
first field). It is not allowed to re-order fields.

  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 11h34.


É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,13784 seconds with 12 queries