Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Weird result from size_t

Reply
Thread Tools

Weird result from size_t

 
 
Rui Maciel
Guest
Posts: n/a
 
      10-31-2009
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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-31-2009
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
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      10-31-2009
On 2009-10-31, Rui Maciel <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      11-01-2009
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-01-2009
jacob navia <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-01-2009
Seebs <(E-Mail Removed)> wrote:
> Rui Maciel <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Strange result from ptrdiff_t and size_t kyagrd@gmail.com C++ 10 02-02-2007 12:17 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments