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 > struct padding ???
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
struct padding ???

Réponse
 
LinkBack Outils de la discussion
Vieux 31/05/2008, 16h39   #1
vikas talwar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut struct padding ???

Hi All,

Can you please explain me how the 'C' compiler allocate memory to
'struct'.
Please go thu the example below and pls suggest me the solution for my
problem.

Here is my structure definition

struct my_dev {
char abc;
} ;

and now when I do
printf("size=%d\n", sizeof(struct my_dev));
output is: 4

But I expect the size of struct = '1'.

Is there any trick to get the struct to occupy memory with pad of 1
byte and not 4 bytes..


--
Thanks
Vikas
  Réponse avec citation
Vieux 31/05/2008, 17h12   #2
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: struct padding ???

vikas talwar <vikas.talwar@gmail.com> wrote:
> Can you please explain me how the 'C' compiler allocate memory to
> 'struct'.
> Please go thu the example below and pls suggest me the solution for my
> problem.


> Here is my structure definition


> struct my_dev {
> char abc;
> } ;


> and now when I do
> printf("size=%d\n", sizeof(struct my_dev));
> output is: 4


> But I expect the size of struct = '1'.


> Is there any trick to get the struct to occupy memory with pad of 1
> byte and not 4 bytes..


The compiler is obviously putting in 3 padding bytes after the
'abc' element of the structure. It does that to make sure when
you create an array of such structures all the structures of
the array end up on correctly aligned memory positions. If
it wouldn't do that something simple as

my_dev xxx[ 2 ];

xxx.abc = 'c';

could lead to a crash of the program, at least on your machine
(on mine the sizeof for the structure is repoted as 1, so on
my machine it doesn't seem necessary to insert padding bytes).
For the same reason also padding bytes can appear between the
elements of a structure.

There is no C standard compliant way to elimintae padding bytes.
Some compilers have some extra options to avoid the addition of
padding bytes, for e.g. gcc you would have to add

__attribute__((packed))

directly after the structure declaration) but, unless you know
exactky what you're doing and the trouble you can get into with
that I can only recommend not to use it. Perhaps you should
tell what exactly you want to achieve that would require the
elimination of padding bytes and perhaps there's a better way
to do it.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
  Réponse avec citation
Vieux 31/05/2008, 20h27   #3
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: struct padding ???

Jens Thoms Toerring <jt@toerring.de> wrote:
> ....it wouldn't do that something simple as


> my_dev xxx[ 2 ];


> xxx.abc = 'c';


Sorry, I meant

xxx[1].abc = 'c'

here, the original version would be a syntax error...

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
  Réponse avec citation
Vieux 31/05/2008, 23h06   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: struct padding ???

jt@toerring.de (Jens Thoms Toerring) writes:
> vikas talwar <vikas.talwar@gmail.com> wrote:
>> Can you please explain me how the 'C' compiler allocate memory to
>> 'struct'.
>> Please go thu the example below and pls suggest me the solution for my
>> problem.

>
>> Here is my structure definition

>
>> struct my_dev {
>> char abc;
>> } ;

>
>> and now when I do
>> printf("size=%d\n", sizeof(struct my_dev));
>> output is: 4

>
>> But I expect the size of struct = '1'.

>
>> Is there any trick to get the struct to occupy memory with pad of 1
>> byte and not 4 bytes..


See questions 2.12 and 2.13 in the comp.lang.c FAQ,
<http://www.c-faq.com/>.

> The compiler is obviously putting in 3 padding bytes after the
> 'abc' element of the structure. It does that to make sure when
> you create an array of such structures all the structures of
> the array end up on correctly aligned memory positions. If
> it wouldn't do that something simple as
>
> my_dev xxx[ 2 ];
>
> xxx.abc = 'c';
>
> could lead to a crash of the program, at least on your machine
> (on mine the sizeof for the structure is repoted as 1, so on
> my machine it doesn't seem necessary to insert padding bytes).
> For the same reason also padding bytes can appear between the
> elements of a structure.


Actually, I can't think of any good reason why the compiler *needs* to
pad "struct my_dev" to 4 bytes. It *could* internally treat a
structure with a single member the same way it would treat an object
of the member type.

There is a requirement for all pointers to structures to have the same
representation; on an implementation where byte pointers and word
pointers are represented differently, that could force 4-byte
alignment for all structures, even small ones. But most systems use
the same representation for all pointers.

Probably the compiler's developers decided that it would be simpler to
force all structures to be word-aligned (where a "word" is probably 4
bytes). They're allowed to do that. The standard allows padding
after members of structures; it doesn't require that padding to be the
minimum necessary. A perverse implementation could add as much
padding as it likes, for no good reason at all.

[...]

For your particular problem, why do you need a structure at all?
Rather than
struct my_dev {
char abc;
};
why not just use type char directly (with a typedef if you like)?
(Using a structure does have the advantage of type safety; "struct
my_dev" is incompatible with other types. But then you have to pay
the price of letting the compiler add extra padding.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  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 13h41.


É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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17059 seconds with 12 queries