GNU C Extensions

Other topics

Attribute packed

packed is a variable attribute that is used with structures and unions in order to minimize the memory requirements.

#include <stdio.h>
struct foo {
    int a;
    char c;
};

struct __attribute__((__packed__))foo_packed {
    int a;
    char c;
};

int main()
{
    printf("Size of foo: %d\n", sizeof(struct foo));
    printf("Size of packed foo: %d\n", sizeof(struct foo_packed));
    return 0;
}

On my 64 bit Linux,

  • Size of struct foo = 8 bytes
  • Size of struct foo_packed = 5 bytes

packed attribute curbs the structure padding that the compiler performs to maintain memory alignment.

Contributors

Topic Id: 10567

Example Ids: 31724

This site is not affiliated with any of the contributors.