This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: structure packing


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]