Re: Packed structs vs. unpacked structs: what's the difference?



Nick Keighley wrote:
pete wrote:
Richard G. Riley wrote:

On 2006-04-09, Daniel Rudy <spamthis@xxxxxxxxxxxx> wrote:


What is the difference between packed and unpacked structs?



The packed keyword

I have no idea on the "standardness" of "packed".

It's not standard C.

...and hence should be avoided if possible. "pragma pack" and their ilk
may slow your program at a slight saving in space and a loss of
portability.

Pragma pack is not often used to save space. Rather it is usually used
as a (non-portable) way to quickly "cast" a byte array (for example a
packet) into a struct. This way you can access data within the packet
directly by simply pointing the struct you want to the first byte of
the packet. This trick is very common in the majority of Unix TCP/IP
stack including Linux & BSD. It is also used in Windows TCP/IP stack
although Microsoft has a different syntax for their version of pragma
pack. But this, of course, is non-standard and is off topic to
comp.lang.c.

.



Relevant Pages

  • Re: standard pragmas?
    ... Of course a differently defined #pragma could, ... `struct S' type, in how many source files do you declare `struct S'? ... header all its subsequent struct declarations were packed, ... and many others like me at lots of customer sites spent tracking down ...
    (comp.lang.c)
  • Re: Problem passing structs to dlls
    ... I tryed your hint, using #pragma pack and got in BOTH, the native and ... It is as if the struct would be cleaned up by the gc if the dll-function ... Then this 815 is visible in the dll. ...
    (microsoft.public.dotnet.languages.vc)
  • pragma pack issue in vstudio 2005
    ... I seem to be running into an issue where I am deriving a new class from a struct with a different alignemnt via the pragma pack, I get a data abort error in the debugger. ... class CDerived: public MyBase ...
    (microsoft.public.vc.language)
  • pragma pack problems
    ... I seem to be running into an issue where I am deriving a new class from a struct with a different alignemnt via the pragma pack, I get a data abort error in the debugger. ... class CDerived: public MyBase ...
    (microsoft.public.windowsce.embedded.vc)