public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: vijay nag <vijunag@gmail.com>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: RE: structure packing
Date: Thu, 16 Jan 2014 10:45:00 -0000	[thread overview]
Message-ID: <CAKhyrx_DW_QNoARLLtBi_YPQ7EvcMB0j9sY-dBJ4b=iqVhho7g@mail.gmail.com> (raw)

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

             reply	other threads:[~2014-01-16 10:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-16 10:45 vijay nag [this message]
2014-01-16 11:34 ` David Brown
     [not found] <ba0bd44d0905210723n5e6e85f5rea8b2b52c381d5d9@mail.gmail.com>
2009-05-21 14:39 ` Mohamed Shafi
2009-05-22  6:39   ` Ian Lance Taylor
2009-05-22  8:01     ` Mohamed Shafi
2009-05-22 13:51       ` Ian Lance Taylor
  -- strict thread matches above, loose matches on Subject: below --
2008-10-20  7:52 Duft Markus
2008-10-20  7:33 Duft Markus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKhyrx_DW_QNoARLLtBi_YPQ7EvcMB0j9sY-dBJ4b=iqVhho7g@mail.gmail.com' \
    --to=vijunag@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).