Re: struct padding ???



vikas talwar <vikas.talwar@xxxxxxxxx> 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@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: struct padding ???
    ... my machine it doesn't seem necessary to insert padding bytes). ... There is a requirement for all pointers to structures to have the same ... representation; on an implementation where byte pointers and word ...
    (comp.lang.c)
  • Re: Does an array of char have alignment issues?
    ... I have no idea what you mean by "tile an array". ... padding bytes after the first char, ... after the last member, ... A union can also ...
    (comp.sys.arm)
  • Re: Question about Declare and Type
    ... Jim Carlock wrote: ... In this particular case, no padding is needed within the struct, except ... that if you declare an array of such, each element of the array will ... but one-byte characters when passed to a Declared function -- ...
    (microsoft.public.vb.general.discussion)
  • Re: Does an array of char have alignment issues?
    ... The struct will have padding bytes so that its fields are aligned. ... Yes, the usual practice is to memcpyeach member of the structure, ... sizeofincludes padding needed to tile an array ...
    (comp.sys.arm)
  • Re: Does an array of char have alignment issues?
    ... I have no idea what you mean by "tile an array". ... padding bytes after the first char, then sizeofwould be 12 because an extra 3 bytes are needed in an array to keep the array elements aligned -- pointers to the unaligned array elements would not work, at least on the ARM. ... after the last member, ... A union can also ...
    (comp.sys.arm)