perils of sizeof

Discussion in 'C Programming' started by Mantorok Redgormor, Jan 25, 2004.

  1. Under c99 one can use %zu to portably print the value returned by
    sizeof, since it is represented by a size_t which is an unsigned inter
    type, you just don't know which unsigned integer type it actually is.

    So under c89, a portable hack for printing the value returned from
    sizeof(since we can't use %zu) would be to use %lu and cast sizeof to
    unsigned long?



    --
    nethlek
     
    Mantorok Redgormor, Jan 25, 2004
    #1
    1. Advertising

  2. "Mantorok Redgormor" <> wrote in message
    news:...
    > Under c99 one can use %zu to portably print the value returned by
    > sizeof, since it is represented by a size_t which is an unsigned inter
    > type, you just don't know which unsigned integer type it actually is.


    The use of z as a modifier was based on a popular extension, so it's not
    uncommon to find it in code that predates the '99 standard.

    > So under c89, a portable hack for printing the value returned from
    > sizeof(since we can't use %zu) would be to use %lu and cast sizeof to
    > unsigned long?


    I'm not sure if you're asking or telling us, but yes. ;)

    In practice you don't always have to use unsigned long in C89. If you know
    the size value is less than 32768 (e.g. very few strings are likely to be
    longer!), then casting to unsigned and using %u is sufficient.

    --
    Peter
     
    Peter Nilsson, Jan 25, 2004
    #2
    1. Advertising

  3. Peter Nilsson wrote:

    > In practice you don't always have to use unsigned long in C89. If you know
    > the size value is less than 32768 (e.g. very few strings are likely to be
    > longer!), then casting to unsigned and using %u is sufficient.


    ITYM less than 65536

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Jan 25, 2004
    #3
  4. "Richard Heathfield" <> wrote in message
    news:...
    > Peter Nilsson wrote:
    >
    > > In practice you don't always have to use unsigned long in C89. If you
    > > know the size value is less than 32768 (e.g. very few strings are
    > > likely to be longer!), then casting to unsigned and using %u is
    > > sufficient.

    >
    > ITYM less than 65536


    Yes, I did mean that. Thanks.

    I was distracted by my parenthetic situation. Whilst size_t's maximum is
    often used, ptrdiff_t is only required to represent offsets in line with
    the minimum object size (for each of the two standards).

    I have written, and have certainly seen, a great many string coding snippets
    that implicitly rely on ptrdiff_t being able to represent the difference
    between two char pointers. Since 32767 is the smallest such difference a
    strictly conforming program can utilise, that's the value I often use to
    represent the largest valid string length.

    I believe that PTRDIFF_MAX is a better bound on object size than SIZE_MAX.
    But I'm probably just being paranoid! ;)

    --
    Peter
     
    Peter Nilsson, Jan 25, 2004
    #4
  5. Mantorok Redgormor

    Jack Klein Guest

    On 24 Jan 2004 23:42:01 -0800, (Mantorok Redgormor)
    wrote in comp.lang.c:

    > Under c99 one can use %zu to portably print the value returned by
    > sizeof, since it is represented by a size_t which is an unsigned inter
    > type, you just don't know which unsigned integer type it actually is.
    >
    > So under c89, a portable hack for printing the value returned from
    > sizeof(since we can't use %zu) would be to use %lu and cast sizeof to
    > unsigned long?


    No problem at all. C89/90 particularly specified, in response to a
    DR, that size_t could not be larger than unsigned long, although this
    is no longer true in C99. So there is no possible loss of information
    in casting a size_t to unsigned long under C89/90.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 25, 2004
    #5
    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. Casey Hawthorne
    Replies:
    14
    Views:
    1,081
    Casey Hawthorne
    Jan 16, 2006
  2. Rhino
    Replies:
    1
    Views:
    523
    Thomas Hawtin
    Apr 14, 2006
  3. Derek
    Replies:
    7
    Views:
    24,370
    Ron Natalie
    Oct 14, 2004
  4. Mantorok Redgormor

    perils of memset

    Mantorok Redgormor, Feb 22, 2004, in forum: C Programming
    Replies:
    2
    Views:
    1,074
    Christian Bau
    Feb 22, 2004
  5. Nick Daly
    Replies:
    1
    Views:
    390
    Steven D'Aprano
    Aug 5, 2009
Loading...

Share This Page