Re: padding mechanism in structures



On Wed, 15 Jun 2005 14:11:40 +0200, Mehta Shailendrakumar wrote:

> see if this helps with sample code attachment
> pack
> #pragma pack( [ n] )

Note that the C language doesn't define a preprocessor directive called
#pragma pack. You compiler may provide one as an extension but you can't
assume that it will exist on other compilers or if it does it will act in
the same way.

> Specifies packing alignment for structure and union members. Whereas the
> packing alignment of structures and unions is set for an entire
> translation unit by the /Zp option,

Again, compiler options are inherently compiler specific. Your compiler
might support such an option but you can't assume that others will. In
particular the original poster's compiler probably doesn't.

> the packing alignment is set at the
> data-declaration level by the pack pragma. The pragma takes effect at
> the first structure or union declaration after the pragma is seen; the
> pragma has no effect on definitions.

Since neither of these are C language features they aren't particularly
relevant to comp.lang.c. Also the question was more about how padding
works than how you might control it. In standard, portable C you can't.

> When you use #pragma pack(n), where n is 1, 2, 4, 8, or 16, each
> structure member after the first is stored on the smaller member type or
> n-byte boundaries. If you use #pragma pack without an argument,
> structure members are packed to the value specified by /Zp. The default
> /Zp packing size is /Zp8.
>
> The compiler also supports the following enhanced syntax:

Again, you mean *your* compiler. Usually the best thing to do with things
like #pragma pack is avoid them studiously since they are inherently
non-portable.

Lawrence
.



Relevant Pages

  • Re: include file question
    ... Where did you read that #pragma once is obsolete? ... because it suggests that what you need is some kind of weird compiler hack to make your ... inside the header file to prevent multiple execution of the contents. ... documentation about Objective C, which is a language which is NOT the C++ language, has ...
    (microsoft.public.vc.mfc)
  • Re: pragma Pack vs. Convention C, portability issue?
    ... -- Can we portably rely on pragma Pack taking precedence ... pragma Convention; ... -- This could be either 8 bits if the compiler lets pragma Pack ...
    (comp.lang.ada)
  • Re: Do not trust ALIGN
    ... #pragma pack ... Although I consider it a BUG in the compiler if those UNALIGNED atributes ... alignment of each member to it's native size. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: include file question
    ... First off I first read that you could keep the compiler ... But then I read that #pragma once is obsolete? ... The #include guards are portable since the beginning of time, ... Note that some headers use neither because they are ...
    (microsoft.public.vc.mfc)
  • Re: Ada checks suppression thanks to compilation options and Ada conformity
    ... One text book describes "pragma" as a "directive to the ... the compiler -- and indicate that compilers are allowed to ignore ... But this doens't apply to pragma Suppress, ... specify a compiler command-line option to suppress all checks, ...
    (comp.lang.ada)