Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return -1 using size_t???

Reply
Thread Tools

return -1 using size_t???

 
 
Barry Schwarz
Guest
Posts: n/a
 
      02-11-2012
On Sat, 11 Feb 2012 11:21:23 -0800 (PST), Robert Hayes
<(E-Mail Removed)> wrote:

>I have read that size_t can hold an unsigned int. I have been playing


size_t is defined as an unsigned integer type but it need not be
unsigned int. I expect on most systems it is unsigned long as
recommended in 7.17-4 of n1256.

--
Remove del for email
 
Reply With Quote
 
 
 
 
Jeroen Mostert
Guest
Posts: n/a
 
      02-11-2012
On 2012-02-11 23:25, Jeffrey R wrote:
> A size_t is an UN-SIGNED type, trying to store a negative number in it is
> a programmer error generating an undefined behavior.


(size_t) -1 is well-defined on any C implementation, per section 6.3.1.3p2
of the standard. It's equal to the maximum value a size_t can hold. This
does not depend on the underlying representation.

There are operations on signed values that invoke undefined behavior (in
fact, integer overflow is the poster child for undefined behavior), but
converting signed values to unsigned values isn't one of them.

--
J.
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      02-11-2012
Devil with the China Blue Dress wrote:
) No, the question was about casting an signed number (-1) to an unsigned type.
) That this can be done and is closed under addition is a property of twos
) complement integers.

No, that this can be done is a result of the wording of the C standard.
Case in point: it also works on ones complement machines.
Twos complement is just a red herring in this case.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-11-2012
Devil with the China Blue Dress <(E-Mail Removed)> writes:

> In article <jh6ps6$8st$(E-Mail Removed)>, Jeffrey R <(E-Mail Removed)>
> wrote:
>
>> It is assuming that the CPU is 2-s compliment.

>
> There is nothing quite as wonderful as a ones complement machine where
> -0==0 and -0<0.


A C implementation would, I think, have to hide that from the
programmer. In C, the value of a negative zero is zero and will
therefore not compare less than zero.

--
Ben.
 
Reply With Quote
 
Jens Gustedt
Guest
Posts: n/a
 
      02-11-2012
Am 02/11/2012 11:31 PM, schrieb Devil with the China Blue Dress:
> No, the question was about casting an signed number (-1) to an unsigned type.
> That this can be done and is closed under addition is a property of twos
> complement integers.


No, the representation of the -1 has nothing do with the result of
converting it into any unsigned integer type. As has been already been
repeated several times upthread, C strictly defines what has to be done
when converting a negative *value* to an unsigned integer type. And in
particular it uses the value and not the representation.

For all conforming C implementations the result of converting -1 to
size_t is SIZE_MAX.

Jens

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-11-2012
Shao Miller <(E-Mail Removed)> writes:

> On 2/11/2012 14:21, Robert Hayes wrote:

<snip>
>> size_t b,c;

<snip>
>> if (c == -1)
>> printf("here\n");

<snip>

> In your comparison before "here," the '-1' is promoted to a 'size_t'
> value for the comparison.


Just a niggle: it's not technically a promotion. The integer promotions
preserve value including sign. What's happening here is the "usual
arithmetic conversions" where a signed int is converted to an unsigned
integer type.

<snip>
--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-11-2012
Robert Hayes <(E-Mail Removed)> writes:

> On Feb 11, 2:07*pm, Eric Sosman <(E-Mail Removed)> wrote:

<snip>
>> * * *size_t cannot hold -1, nor any other negative value. *It can,
>> however, hold the "wrapped around" value that results from converting
>> -1 to size_t; this value is large and positive. *You can, if you
>> like, impute some special meaning to that large value, just as you
>> might treat zero as special, or -42 (in a signed type). *Just don't
>> expect `(size_t)-1' to be negative, because it ain't.

<snip>

> Thank you for the clear explanation. I will not use a size_t to hold
> a negative number.


Don't worry you can't! But I think I get what you mean. However, I
don't think that's the take away message here. It's not uncommon to use
(size_t)-1 as a special signifier. Sometimes size_t is the natural
return type and yet you want to use an in-band error indicator.
(size_t)-1 is good for the job. In fact, C defines several standard
functions that do this. A couple of them even signal two different
errors using (size_t)-1 and (size_t)-2.

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-12-2012
Malcolm McLean <(E-Mail Removed)> writes:
> On Feb 11, 8:42*pm, Robert Hayes <(E-Mail Removed)> wrote:
>>
>> Thank you for the clear explanation. *I will not use a size_t to hold
>> a negative number.- Hide quoted text -
>>

> There's an example of a size_t holding a negative number on the thread
> about stripping right spaces.


No, there isn't.

I don't recall the details from that thread. The point is that size_t
simply cannot hold a negative number.

You can convert a negative number (of a signed type) to size_t, and
store the result of the conversion in a size_t object. You *probably*
can then convert the stored value back to the signed type, but the
result is implementation-defined.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-12-2012
Barry Schwarz <(E-Mail Removed)> writes:
> On Sat, 11 Feb 2012 11:21:23 -0800 (PST), Robert Hayes
> <(E-Mail Removed)> wrote:
>>I have read that size_t can hold an unsigned int. I have been playing

>
> size_t is defined as an unsigned integer type but it need not be
> unsigned int. I expect on most systems it is unsigned long as
> recommended in 7.17-4 of n1256.


That paragraph doesn't recommend using unsigned long; it recommends
*not* using a type with conversion rank greater than that of
unsigned long unless it's necessary. An implementation defining
size_t as either unsigned int or unsigned long would satisfy the
recommendation.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      02-12-2012
On Feb 12, 1:10*am, Keith Thompson <(E-Mail Removed)> wrote:
> Malcolm McLean <(E-Mail Removed)> writes:
> > On Feb 11, 8:42*pm, Robert Hayes <(E-Mail Removed)> wrote:

>
> >> Thank you for the clear explanation. *I will not use a size_t to hold
> >> a negative number.- Hide quoted text -

>
> > There's an example of a size_t holding a negative number on the thread
> > about stripping right spaces.

>
> No, there isn't.
>
> I don't recall the details from that thread. *The point is that size_t
> simply cannot hold a negative number.
>


Here it is
[ from Ben ]
size_t i = strlen(s);
while (i-- > 0 && isspace(s[i]))
/* do nothing */;
s[i + 1] = '\0';

If the string is all spaces, or empty, I'd say that i goes to -1.
--
Malcolm's website
http://www.malcolmmclean.site11.com/www



 
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
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments