Packed structure???

Programming, for all ages and all languages.
Post Reply
NOTNULL

Packed structure???

Post by NOTNULL »

Hi all,
What is a packed structure? What is the keyword to declare a packed structure? How the memory is allocated for a packed structure member variables?
AR

Re:Packed structure???

Post by AR »

A packed structure (as I understand it) is a struct which is not aligned. Compilers will generally align structs to speed up the access time on variables, on an x86 computer the alignment is 4 bytes (32bits).

Code: Select all

struct Example
{
    char v1;  /* 4 bytes, only first 1 byte is used */
    short v2; /* 4 bytes, only first 2 bytes are used */
    int v3; /* 4 bytes, all 4 are used */
}; /* 12 bytes in total */
Essentially packing just removes the padding:

Code: Select all

struct Example
{
    char v1;  /* 1 byte */
    short v2; /* 2 bytes */
    int v3; /* 4 bytes */
} __attribute__((__packed__)); /* Struct only uses 7 bytes instead of 12 now */
Packing structures is generally only used in cases where you are following a standard which requires the data be aligned in a specific way. Removing the alignment will reduce the access speed for the data as the compiler will have to add code to perform bit shifting on every access which would not be necessary if it was aligned.
mystran

Re:Packed structure???

Post by mystran »

Actually, in the case of x86 series, as long as we only pack bytes (and not individual bits), and ignoring certain special opcodes that you mostly can't do from C anyway, there isn't any extra code that needs to be generated.

On many RISC architectures such extra code could be needed, but IA-32 processors happily let you fetch from unaligned locations with two caveats:

- unaligned access is slower, since the processor still need to do the bit shifting internally (AFAIK) and possibly needs to do two separate requests on memory, and could get two cache misses instead of one, and in the really extreme case even two page faults. In short it still gets messy internally.

- if alignment check is enabled, then an alignment check exception is generated, which the operating system might or might not actually let you handle yourself. Then again, any sane system set's alignment check disabled by default for C code, so you'd need to enable it first to get any problems. Then again, normal application code can disable/enable alignment check (one bit in EFLAGS) if it wants to, assuming the OS allows this by setting the AM bit in CR0.
NOTNULL

Re:Packed structure???

Post by NOTNULL »

Well, does this means that moving a character pointer takes more time than moving an integer pointer?
AR

Re:Packed structure???

Post by AR »

A pointer is always 32bits (unless you're in real mode, than the pointer is always 16bits). Pointers are the same as the address space (64bit CPU = 64bit pointers), I think there is some weird architecture that has 2 different pointers but anyway.

Moving the pointer doesn't make any difference as the pointer is 32bits to start with [provided it isn't inside a packed struct], however accessing an array of char will not be aligned and will likely therefore be slower.
mystran

Re:Packed structure???

Post by mystran »

There are several kinds of weird architectures and one of the most weird is x86 in real-mode which has 16-bit "near" pointers, and 2x16-bit "far" pointers, and the "far" pointer gets it's address in the most weird possible way:

segment*16 + offset = address

which means that the addressing is 20-bit, which it definitely the most weird part of the whole thing.
AR

Re:Packed structure???

Post by AR »

I was thinking more along the lines of an architecture with 2 pointers (code specific and data specific). I hate segmentation just as much as the next person but it isn't that hard to get the hang of.
Post Reply