structure packing

vijay nag vijunag@gmail.com
Thu Jan 16 10:45:00 GMT 2014


Dear gcc,

Why is the size of below struct UnPackedStruct 6 ? Is there any switch
similar to -E in gcc to check how data alignment is performed by GCC
for structs ?


#include <stdio.h>
#include <stdint.h>

struct tlv_header_t {
    unsigned char tlv_length_upper : 3;
    unsigned char reserved : 5;
    unsigned char tlv_length_lower;
    uint16_t tlv_type;
};

typedef struct {
  struct tlv_header_t  sess;
  uint8_t p;
} UnPackedStruct;

typedef struct {
   int a;
   char b;
} PaddedStruct;

int main()
{
  UnPackedStruct a;
  PaddedStruct b;

  printf("size of a = %u\n", sizeof(a));
  printf("size of b = %u\n", sizeof(b));

  return 1;
}

./padding
size of a = 6
size of b = 8



More information about the Gcc-help mailing list