Viewing the padded bytes

Discussion in 'C Programming' started by karthikbalaguru, Jan 14, 2009.

  1. Hi ,

    I have the below structure.
    struct check_struct{
    double a;
    char b;
    };
    My system reports the size of the above structure as 16 bytes.

    I understand that there is some padding at the end of the above
    structure and hence the size gets calculated to 16 bytes.
    But, how to view the data/info that are padded using a debugger
    like gdb or visual c++ debugger ?

    I used watch windows but, it did not show the padded data .
    Any ideas ?

    Thx in advans,
    Karthik Balaguru
    karthikbalaguru, Jan 14, 2009
    #1
    1. Advertising

  2. karthikbalaguru

    user923005 Guest

    On Jan 14, 12:41 am, karthikbalaguru <>
    wrote:
    > Hi ,
    >
    > I have the below structure.
    > struct check_struct{
    >         double a;
    >         char b;};
    >
    > My system reports the size of the above structure as 16 bytes.
    >
    > I understand that there is some padding at the end of the above
    > structure and hence the size gets calculated to 16 bytes.
    > But, how to view the data/info that are padded using a debugger
    > like gdb or visual c++ debugger ?
    >
    > I used watch windows but, it did not show the padded data .
    > Any ideas ?


    typedef struct check_struct {
    double a;
    char b;
    } check_struct_type;

    check_struct_type cs = {0};
    unsigned char *csp = (unsigned char *) &cs;

    Why do you care? You cannot depend on anything about the padding
    bytes.
    They might contain all zeros on one invocation and 0xDEADBEE on
    another.
    user923005, Jan 14, 2009
    #2
    1. Advertising

  3. karthikbalaguru

    David Webber Guest

    "karthikbalaguru" <> wrote in message
    news:...

    > I have the below structure.
    > struct check_struct{
    > double a;
    > char b;
    > };
    > My system reports the size of the above structure as 16 bytes.
    >
    > I understand that there is some padding at the end of the above
    > structure and hence the size gets calculated to 16 bytes.
    > But, how to view the data/info that are padded using a debugger
    > like gdb or visual c++ debugger ?
    >
    > I used watch windows but, it did not show the padded data .
    > Any ideas ?


    I don't like having structures a different size from what I see on the page.
    It makes me nervous. So I would always write the above as

    struct check_struct
    {
    double a;
    char b;
    char reserved[7];
    };

    If you do that you can see the padded bytes.

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 14, 2009
    #3
  4. karthikbalaguru

    Richard Guest

    "David Webber" <> writes:

    > "karthikbalaguru" <> wrote in message
    > news:...
    >
    >> I have the below structure.
    >> struct check_struct{
    >> double a;
    >> char b;
    >> };
    >> My system reports the size of the above structure as 16 bytes.
    >>
    >> I understand that there is some padding at the end of the above
    >> structure and hence the size gets calculated to 16 bytes.
    >> But, how to view the data/info that are padded using a debugger
    >> like gdb or visual c++ debugger ?
    >>
    >> I used watch windows but, it did not show the padded data .
    >> Any ideas ?

    >
    > I don't like having structures a different size from what I see on the
    > page. It makes me nervous. So I would always write the above as
    >
    > struct check_struct
    > {
    > double a;
    > char b;
    > char reserved[7];
    > };
    >
    > If you do that you can see the padded bytes.
    >
    > Dave


    Assuming things it shouldn't - I dont like that at all.

    Those bytes are not reserved either - they are padding. Two totally
    different things I think.

    To the OP. Use the debugger and examine the memory at the address of the
    struct. e.g in gdb to see 8 hex words

    (gdb)x/8x &myStruct



    --
    I'm not a person who particularly had heros when growing up.
    - What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c.
    Richard, Jan 14, 2009
    #4
  5. karthikbalaguru

    Guest

    On 14 Jan, 13:37, Richard <> wrote:

    <snip>

    > --
    > I'm not a person who particularly had heros when growing up.
    >  -  What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c


    I thought you were going to fix your misleading sig?
    , Jan 14, 2009
    #5
  6. karthikbalaguru

    Guest

    On 14 Jan, 13:37, Richard <> wrote:
    > "David Webber" <> writes:
    > > "karthikbalaguru" <> wrote in message
    > >news:...

    >
    > >> I have the below structure.
    > >> struct check_struct{
    > >> double a;
    > >> char b;
    > >> };
    > >> My system reports the size of the above structure as 16 bytes.

    >
    > >> I understand that there is some padding at the end of the above
    > >> structure and hence the size gets calculated to 16 bytes.
    > >> But, how to view the data/info that are padded using a debugger
    > >> like gdb or visual c++ debugger ?

    >
    > >> I used watch windows but, it did not show the padded data .
    > >> Any ideas ?

    >
    > > I don't like having structures a different size from what I see on the
    > > page. It makes me nervous.   So I would always write the above as

    >
    > > struct check_struct
    > > {
    > >    double a;
    > >    char b;
    > >    char reserved[7];
    > > };

    >
    > > If you do that you can see the padded bytes.

    >
    > > Dave

    >
    > Assuming things it shouldn't - I dont like that at all.
    >
    > Those bytes are not reserved either - they are padding. Two totally
    > different things I think.
    >
    > To the OP. Use the debugger and examine the memory at the address of the
    > struct. e.g in gdb to see 8 hex words
    >
    > (gdb)x/8x &myStruct
    >
    > --
    > I'm not a person who particularly had heros when growing up.
    >  -  What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c


    oh sugar. ok *now* I've read what it actually says. sorry
    , Jan 14, 2009
    #6
  7. karthikbalaguru

    Guest

    On 14 Jan, 13:00, "David Webber" <>
    wrote:
    > "karthikbalaguru" <> wrote in message
    > news:...


    > > I have the below structure.
    > > struct check_struct{
    > > double a;
    > > char b;
    > > };
    > >
    > > My system reports the size of the above structure as 16 bytes.

    >
    > > I understand that there is some padding at the end of the above
    > > structure and hence the size gets calculated to 16 bytes.
    > > But, how to view the data/info that are padded using a debugger
    > > like gdb or visual c++ debugger ?


    why do you need to see the padding bytes?

    > > I used watch windows but, it did not show the padded data .
    > > Any ideas ?

    >
    > I don't like having structures a different size from what I see on the page.
    > It makes me nervous.  


    stop programming in C then.


    > So I would always write the above as
    >
    > struct check_struct
    > {
    >     double a;
    >     char b;
    >     char reserved[7];
    >
    > };
    >
    > If you do that you can see the padded bytes.


    what happens when they change?
    , Jan 14, 2009
    #7
  8. karthikbalaguru

    Guest

    On Jan 14, 8:00 am, "David Webber" <>
    wrote:
    > "karthikbalaguru" <> wrote in message
    >
    > news:...
    >
    > > I have the below structure.
    > > struct check_struct{
    > > double a;
    > > char b;
    > > };
    > > My system reports the size of the above structure as 16 bytes.

    >
    > > I understand that there is some padding at the end of the above
    > > structure and hence the size gets calculated to 16 bytes.
    > > But, how to view the data/info that are padded using a debugger
    > > like gdb or visual c++ debugger ?

    >
    > > I used watch windows but, it did not show the padded data .
    > > Any ideas ?

    >
    > I don't like having structures a different size from what I see on the page.
    > It makes me nervous.   So I would always write the above as
    >
    > struct check_struct
    > {
    >     double a;
    >     char b;
    >     char reserved[7];
    >
    > };
    >
    > If you do that you can see the padded bytes.


    I don't think so. It will always show you the reserved member's
    contents but I think the implementation is still free to insert
    padding in any amount and at any location in this structure it
    pleases, except at the beggining.

    >
    > Dave
    > --
    > David Webber
    > Author of 'Mozart the Music Processor'http://www.mozart.co.uk
    > For discussion/support seehttp://www.mozart.co.uk/mozartists/mailinglist.htm
    , Jan 14, 2009
    #8
  9. karthikbalaguru

    Richard Guest

    writes:

    > On 14 Jan, 13:37, Richard <> wrote:
    >
    > <snip>
    >
    >> --
    >> I'm not a person who particularly had heros when growing up.
    >>  -  What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c

    >
    > I thought you were going to fix your misleading sig?
    >


    I thought you were going to stop being an idiot and read it. Or are you
    going to do a Heathfield and start demanding apologies for other people?

    --
    I'm not a person who particularly had heros when growing up.
    - What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c.
    Richard, Jan 14, 2009
    #9
  10. karthikbalaguru

    Richard Guest

    writes:

    > On 14 Jan, 13:37, Richard <> wrote:
    >> "David Webber" <> writes:
    >> > "karthikbalaguru" <> wrote in message
    >> >news:...

    >>
    >> >> I have the below structure.
    >> >> struct check_struct{
    >> >> double a;
    >> >> char b;
    >> >> };
    >> >> My system reports the size of the above structure as 16 bytes.

    >>
    >> >> I understand that there is some padding at the end of the above
    >> >> structure and hence the size gets calculated to 16 bytes.
    >> >> But, how to view the data/info that are padded using a debugger
    >> >> like gdb or visual c++ debugger ?

    >>
    >> >> I used watch windows but, it did not show the padded data .
    >> >> Any ideas ?

    >>
    >> > I don't like having structures a different size from what I see on the
    >> > page. It makes me nervous.   So I would always write the above as

    >>
    >> > struct check_struct
    >> > {
    >> >    double a;
    >> >    char b;
    >> >    char reserved[7];
    >> > };

    >>
    >> > If you do that you can see the padded bytes.

    >>
    >> > Dave

    >>
    >> Assuming things it shouldn't - I dont like that at all.
    >>
    >> Those bytes are not reserved either - they are padding. Two totally
    >> different things I think.
    >>
    >> To the OP. Use the debugger and examine the memory at the address of the
    >> struct. e.g in gdb to see 8 hex words
    >>
    >> (gdb)x/8x &myStruct
    >>
    >> --
    >> I'm not a person who particularly had heros when growing up.
    >>  -  What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c

    >
    > oh sugar. ok *now* I've read what it actually says. sorry
    >


    heh :-; No harm. But I did reply before I saw your acknowledgement.


    --
    I'm not a person who particularly had heros when growing up.
    - What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c.
    Richard, Jan 14, 2009
    #10
  11. In article <>,
    David Webber <> wrote:

    >I don't like having structures a different size from what I see on the page.
    >It makes me nervous. So I would always write the above as
    >
    >struct check_struct
    >{
    > double a;
    > char b;
    > char reserved[7];
    >};


    Are you sure every compiler uses 7 bytes of padding?

    -- Richard
    --
    Please remember to mention me / in tapes you leave behind.
    Richard Tobin, Jan 14, 2009
    #11
  12. On Jan 14, 12:41 am, karthikbalaguru <>
    wrote:

    > Hi ,
    >
    > I have the below structure.
    > struct check_struct{
    >         double a;
    >         char b;};
    >
    > My system reports the size of the above structure as 16 bytes.


    > I understand that there is some padding at the end of the above
    > structure and hence the size gets calculated to 16 bytes.


    How do you know it's padding. Maybe a 'double' is 15 bytes on your
    platform.

    > But, how to view the data/info that are padded using a debugger
    > like gdb or visual c++ debugger ?
    >
    > I used watch windows but, it did not show the padded data .
    > Any ideas ?


    You are programming in C, there is no reason you should have to know
    or care.

    Unless you are doing something very unusual, you are barking up the
    wrong tree.

    An example of a case where you could reasonably care would be one
    where your memory allocator specifically sets assigned memory to a
    particular value and freed memory to some other value. By seeing the
    bytes that would presumably only be touched by the allocator, you can
    tell if the structure was freed.

    In any event, I've never heard of a debugger that doesn't let you view
    any chunk of memory you want to view.

    DS
    David Schwartz, Jan 14, 2009
    #12
  13. karthikbalaguru

    user923005 Guest

    On Jan 14, 8:08 am, (Richard Tobin) wrote:
    > In article <>,
    >
    > David Webber <> wrote:
    > >I don't like having structures a different size from what I see on the page.
    > >It makes me nervous.   So I would always write the above as

    >
    > >struct check_struct
    > >{
    > >    double a;
    > >    char b;
    > >    char reserved[7];
    > >};

    >
    > Are you sure every compiler uses 7 bytes of padding?


    One of mine uses 0, 2, 4, 8, or 16 bytes, depending on what I ask for.
    user923005, Jan 14, 2009
    #13
  14. karthikbalaguru

    David Webber Guest

    <> wrote in message
    news:...
    On 14 Jan, 13:00, "David Webber" <>
    wrote:

    >> I don't like having structures a different size from what I see on the
    >> page.
    >> It makes me nervous.


    > stop programming in C then.


    Why? [Actually I did, many years ago - I do it in C++ these days.]


    >> So I would always write the above as
    >>
    >> struct check_struct
    >> {
    >> double a;
    >> char b;
    >> char reserved[7];
    >>
    >> };
    >>
    >> If you do that you can see the padded bytes.


    >what happens when they change?


    Should I care? Most importantly it reminds me, when I want to make the
    structure bigger, not to write

    struct check_struct
    {
    double a;
    char b;
    double c;
    };

    which is truly hideous. [And anyone who does program in C or C++ should be
    close enough to what's going on underneath *never* to write that.]

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 15, 2009
    #14
  15. karthikbalaguru

    David Webber Guest

    <> wrote in message
    news:...
    On Jan 14, 8:00 am, "David Webber" <>
    wrote:

    > struct check_struct
    > {
    > double a;
    > char b;
    > char reserved[7];
    >
    > };
    >
    > If you do that you can see the padded bytes.


    > I don't think so. It will always show you the reserved member's

    contents but I think the implementation is still free to insert
    padding in any amount and at any location in this structure it
    pleases, except at the beggining.<

    I dare say, in principle, but I use compiler options which specify the
    padding properties, and it would be unusual for 8 byte boundaries not to be
    safe.

    [I have large arrays of structures with many bit fields, and it is in my
    interest, to arrange them so that padding is unnecessary, and so I control
    very carefully where the bit fields fit in relation to byte and word
    boundaries. It's a useful principle of "defensive programming".]

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 15, 2009
    #15
  16. karthikbalaguru

    David Webber Guest

    "Richard Tobin" <> wrote in message
    news:gkl2m2$15ii$...
    > In article <>,
    > David Webber <> wrote:
    >
    >>I don't like having structures a different size from what I see on the
    >>page.
    >>It makes me nervous. So I would always write the above as
    >>
    >>struct check_struct
    >>{
    >> double a;
    >> char b;
    >> char reserved[7];
    >>};

    >
    > Are you sure every compiler uses 7 bytes of padding?


    When I said "always", it was an example. :) I use compiler options to
    specify such options. [I don't know that *all* compilers allow this, but
    the ones I have tried all do IIRC.] So in some cases I'd probably get away
    with:

    struct check_struct
    {
    double a;
    char b;
    char reserved;
    };

    (with a 32 bit compiler).

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 15, 2009
    #16
  17. karthikbalaguru

    David Webber Guest

    "Phil Da Lick!" <> wrote in
    message news:...

    > Indeed. That's an assumption that shouldn't be made.


    True.

    >You don't know what kind of optimisations you might get. Best approach for
    >struct padding is to ignore it and let the compiler optimise as it sees
    >fit.


    Definitely not. The best approach is to make life easy for the compiler
    and check that you are doing it optimally. It isn't hard and you end up
    with much more efficient code.

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 15, 2009
    #17
  18. karthikbalaguru

    David Webber Guest

    "user923005" <> wrote in message
    news:...
    On Jan 14, 8:08 am, (Richard Tobin) wrote:

    > One of mine uses 0, 2, 4, 8, or 16 bytes, depending on what I ask for.


    Yes, but packing on single byte boundaries is probably not the most
    efficient when compiling for a 32 bit machine. 4 byte boundaries is
    probably a better choice.

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 15, 2009
    #18
  19. karthikbalaguru

    David Webber Guest

    "Richard Tobin" <> wrote in message
    news:gkl2m2$15ii$...
    > In article <>,
    > David Webber <> wrote:
    >
    >>I don't like having structures a different size from what I see on the
    >>page.
    >>It makes me nervous. So I would always write the above as
    >>
    >>struct check_struct
    >>{
    >> double a;
    >> char b;
    >> char reserved[7];
    >>};

    >
    > Are you sure every compiler uses 7 bytes of padding?


    When I said "always", it was an example. :) The OP said he got 16
    bytes, so his compiler is adding 7: I just made them explicit.

    I use compiler options to specify such options, and usually set it to 4-byte
    boundaries for 32 bit machines. [I don't know that *all* compilers allow
    this, but
    the ones I have tried all do IIRC.] With appropriate choices one could
    probably get away
    with:

    struct check_struct
    {
    double a;
    char b;
    char reserved;
    };

    (with a 32 bit compiler).

    Dave
    --
    David Webber
    Author of 'Mozart the Music Processor'
    http://www.mozart.co.uk
    For discussion/support see
    http://www.mozart.co.uk/mozartists/mailinglist.htm
    David Webber, Jan 15, 2009
    #19
  20. karthikbalaguru

    cc Guest

    On Jan 14, 8:52 am, wrote:
    > On 14 Jan, 13:00, "David Webber" <>
    > wrote:
    >
    > > "karthikbalaguru" <> wrote in message
    > >news:...
    > > > I have the below structure.
    > > > struct check_struct{
    > > > double a;
    > > > char b;
    > > > };

    >
    > > > My system reports the size of the above structure as 16 bytes.

    >
    > > > I understand that there is some padding at the end of the above
    > > > structure and hence the size gets calculated to 16 bytes.
    > > > But, how to view the data/info that are padded using a debugger
    > > > like gdb or visual c++ debugger ?

    >
    > why do you need to see the padding bytes?
    >
    > > > I used watch windows but, it did not show the padded data .
    > > > Any ideas ?

    >
    > > I don't like having structures a different size from what I see on the page.
    > > It makes me nervous.  

    >
    > stop programming in C then.
    >
    > > So I would always write the above as

    >
    > > struct check_struct
    > > {
    > >     double a;
    > >     char b;
    > >     char reserved[7];

    >
    > > };

    >
    > > If you do that you can see the padded bytes.

    >
    > what happens when they change?


    Why don't you retards keep it in clc?
    cc, Jan 15, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Dalberg
    Replies:
    0
    Views:
    460
    John Dalberg
    Jan 21, 2005
  2. Replies:
    0
    Views:
    22,503
  3. Roy Smith
    Replies:
    2
    Views:
    1,512
    Alan Moore
    May 3, 2004
  4. Replies:
    4
    Views:
    381
    Thomas Hawtin
    Aug 3, 2005
  5. Davide Bruzzone
    Replies:
    9
    Views:
    366
    Adam S. Roan
    Aug 27, 2003
Loading...

Share This Page