sizeof unpadded struct size

Discussion in 'C Programming' started by George Orwell, Dec 17, 2008.

  1. Peter Nilsson wrote:
    > int padded = sizeof (struct abc);
    > int unpadded = pp_struct_abc(pp_as_type_size, +);
    >
    > printf(" padded: %d bytes\n", padded);
    > printf("unpadded: %d bytes\n", unpadded);
    > printf(" padding: %d bytes\n", padded - unpadded);


    Please fix your code.

    The sizeof operator returns a size_t.

    You've been on this newsgroup for many years, so surely
    you're aware by now of the *grave* importance that
    Keith Thompson & Co. place on proper sizeof typing. Or
    is it important for only newcomers to the group, and not
    the clique's foot soldiers?

    Only the other day, in the thread "size of a struct without
    'sizeof'", Keith Thompson & Co. reminded us that, even though
    the structure presented by a poster had very few members,
    there could be problems because, I quote:

    "... but even this will fail if the struct's size is greater
    than INT_MAX, which could be as low as 32767. It would be
    better to use a `size_t' variable."
    -- Eric Sosman

    "It's possible (if unlikely) that sizeof(struct whatever)
    could exceed INT_MAX."
    -- Keith Thompson

    You've been warned, Peter! Now get fixing!

    Yours,
    Han from China

    Il mittente di questo messaggio|The sender address of this
    non corrisponde ad un utente |message is not related to a real
    reale ma all'indirizzo fittizio|person but to a fake address of an
    di un sistema anonimizzatore |anonymous system
    Per maggiori informazioni |For more info
    https://www.mixmaster.it
     
    George Orwell, Dec 17, 2008
    #1
    1. Advertising

  2. George Orwell <> wrote:
    > Peter Nilsson wrote:
    > > int   padded = sizeof (struct abc);
    > > int unpadded = pp_struct_abc(pp_as_type_size, +);
    > >
    > > printf("  padded: %d bytes\n", padded);
    > > printf("unpadded: %d bytes\n", unpadded);
    > > printf(" padding: %d bytes\n", padded - unpadded);

    >
    > Please fix your code.


    If it is broken, then it is broken in the struct definition
    which you snipped.

    <snip>
    > "... but even this will fail if the struct's size is
    > greater than INT_MAX, which could be as low as 32767.
    > It would be better to use a `size_t' variable."
    >     -- Eric Sosman
    >
    > "It's possible (if unlikely) that sizeof(struct
    > whatever) could exceed INT_MAX."
    >     -- Keith Thompson
    >
    > You've been warned, Peter!


    I've issued the warning myself. But I'm only responsible
    for my own education. I'm not responsible for others.

    > Now get fixing!


    There's nothing to fix. Rather, it's not possible to
    fix the true problem.

    Under C90, implementations are only required to support
    one object of 32767 bytes in size. [And then, only in a
    specific program of the implementation's choosing.] If
    you create a struct larger than 32767 bytes, then you
    have already crimped its portability.

    You can't control how much padding an implementation will
    add in a struct. Given the sample struct had two ints and
    char, I have to gample on QoI that I have not exceeded the
    size limit of the implementation.

    True, not using size_t adds another gamble, but sometimes
    I just roll the dice.

    --
    Peter
     
    Peter Nilsson, Dec 17, 2008
    #2
    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. Derek
    Replies:
    7
    Views:
    24,416
    Ron Natalie
    Oct 14, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,438
    Chris Fogelklou
    Apr 20, 2004
  3. srikar2097

    size of a struct without "sizeof"

    srikar2097, Dec 15, 2008, in forum: C Programming
    Replies:
    25
    Views:
    964
    Keith Thompson
    Dec 15, 2008
  4. sinbad

    sizeof unpadded struct size

    sinbad, Dec 16, 2008, in forum: C Programming
    Replies:
    27
    Views:
    1,686
    Ben Bacarisse
    Dec 19, 2008
  5. Nomen Nescio

    sizeof unpadded struct size

    Nomen Nescio, Dec 16, 2008, in forum: C Programming
    Replies:
    0
    Views:
    301
    Nomen Nescio
    Dec 16, 2008
Loading...

Share This Page