Weird result from size_t

Discussion in 'C Programming' started by Rui Maciel, Oct 31, 2009.

  1. Rui Maciel

    Rui Maciel Guest

    It's expected that size_t is an unsigned integral type and therefore it has been suggested that (size_t)-1 is
    a good, standard-ish way to determine the maximum value representable by a size_t. So I ran the following
    code:

    <code>
    #include <stdio.h>

    int main(void)
    {
    printf("size: %zd\n", (size_t)-1);
    return 0;
    }
    </code>

    What I didn't expected was the following output:

    <shell>
    525 rui@kubuntu:temp$ gcc main.c
    526 rui@kubuntu:temp$ ./a.out
    size: -1

    </shell>


    Is this a GCC bug or am I missing out something?


    Rui Maciel
    Rui Maciel, Oct 31, 2009
    #1
    1. Advertising

  2. Rui Maciel

    jacob navia Guest

    Rui Maciel a écrit :
    > It's expected that size_t is an unsigned integral type and therefore it has been suggested that (size_t)-1 is
    > a good, standard-ish way to determine the maximum value representable by a size_t. So I ran the following
    > code:
    >
    > <code>
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > printf("size: %zd\n", (size_t)-1);
    > return 0;
    > }
    > </code>
    >
    > What I didn't expected was the following output:
    >
    > <shell>
    > 525 rui@kubuntu:temp$ gcc main.c
    > 526 rui@kubuntu:temp$ ./a.out
    > size: -1
    >
    > </shell>
    >
    >
    > Is this a GCC bug or am I missing out something?
    >
    >
    > Rui Maciel


    Using lcc-win (gcc should be the same)

    #include <stdio.h>

    int main(void)
    {
    printf("size: %zd\n", (size_t)-1);
    printf("size: %zu\n", (size_t)-1);
    return 0;
    }

    Output:
    size: -1
    size: 4294967295
    jacob navia, Oct 31, 2009
    #2
    1. Advertising

  3. Rui Maciel

    Seebs Guest

    On 2009-10-31, Rui Maciel <> wrote:
    > It's expected that size_t is an unsigned integral type and therefore it has been suggested that (size_t)-1 is
    > a good, standard-ish way to determine the maximum value representable by a size_t. So I ran the following
    > code:


    Hee hee.

    > printf("size: %zd\n", (size_t)-1);


    %zd prints a *signed* value of the same size as a size_t.

    Try %zu.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Oct 31, 2009
    #3
  4. Rui Maciel

    Rui Maciel Guest

    jacob navia wrote:

    > Using lcc-win (gcc should be the same)
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > printf("size: %zd\n", (size_t)-1);
    > printf("size: %zu\n", (size_t)-1);
    > return 0;
    > }
    >
    > Output:
    > size: -1
    > size: 4294967295


    Nice. I see it does in fact work. Thanks for the help, everyone.


    Rui Maciel
    Rui Maciel, Nov 1, 2009
    #4
  5. jacob navia <> writes:
    [...]
    > Using lcc-win (gcc should be the same)
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > printf("size: %zd\n", (size_t)-1);
    > printf("size: %zu\n", (size_t)-1);
    > return 0;
    > }
    >
    > Output:
    > size: -1
    > size: 4294967295


    Strictly speaking, the behavior of
    printf("size: %zd\n", (size_t)-1);
    is undefined. It expects a value of the signed integer type
    corresponding to size_t, but you're giving it a value of type
    size_t, and a value that's outside the representable range of the
    signed type.

    In practice, you're very likely to get the "-1" output you're
    seeing. You're *converting* the int value -1 to size_t, and then
    *reinterpreting* the resulting bit pattern as if it were of a signed
    type. On most systems, the conversion and the reinterpretation
    will be inverse operations (I'm glossing over the fact that int
    and size_t could be different sizes, but the result is the same).

    There are cases where you can get away with using a printf format
    that doesn't exactly match the type of the argument, but I find
    it much easier to be consistent (using casts if necessary) than to
    figure out the cases where you don't have to be.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 1, 2009
    #5
  6. Seebs <> wrote:
    > Rui Maciel <> wrote:
    > >         printf("size: %zd\n", (size_t)-1);

    >
    > %zd prints a *signed* value of the same size as a size_t.


    Strictly speaking, it expects and prints the corresponding
    signed type for size_t. There may be multiple signed integer
    types with the same 'size' as size_t, but %zd isn't
    guaranteed to work for them.

    --
    Peter
    Peter Nilsson, Nov 1, 2009
    #6
    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. Replies:
    10
    Views:
    602
    red floyd
    Feb 2, 2007
  2. Alex Vinokur
    Replies:
    9
    Views:
    787
    James Kanze
    Oct 13, 2008
  3. J.Ram
    Replies:
    7
    Views:
    650
  4. Alex Vinokur
    Replies:
    1
    Views:
    575
  5. Michael Tan
    Replies:
    32
    Views:
    966
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page