Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: size_t

Thread Tools

Re: size_t

Dan Pop
Posts: n/a
In <(E-Mail Removed)> "Arthur J. O'Dwyer" <(E-Mail Removed)> writes:

>On Mon, 23 Jun 2003, Peter Nilsson wrote:
>> Kalle Olavi Niemitalo <(E-Mail Removed)> wrote...
>> > "Arthur J. O'Dwyer" writes:
>> >
>> > > int c;
>> > > while ((c=getchar()) != EOF) {
>> > > if (isalpha((unsigned char)c)) ...
>> > > }
>> > >
>> > > doesn't invoke undefined behavior for any user input,
>> > > even the ones where c is, say, a (non-EOF) negative number.

>> The (unsigned char) cast fixes nothing since c is derived from
>> getchar().

>I.e., the cast is not needed, because fgetc() returns "an unsigned char
>converted to an int". I had not known that; thanks. However, I don't
>see any similar guarantee for the chars returned by fgets(), so just
>make an appropriate substitution, or tell me I'm wrong again.

fgets() doesn't return any chars, it returns a pointer.

>> > I don't think getchar() can return a non-EOF negative number,
>> > unless INT_MAX < UCHAR_MAX which AFAIK doesn't occur on any
>> > hosted implementation.

>You're correct. But in the case of fgets() and friends, unless I'm
>mistaken, there's nothing prohibiting the user from inputting negative
>values directly (by whatever means his system does input), if 'char' is
>a signed type on his implementation.
>Does it say anywhere that fgets() must act as if each input character was
>read via fgetc()?

Yes (see the quote in my other post), but this doesn't solve the problem
if plain char is signed and cannot represent the value returned by fgetc.

When calling <ctype.h> functions on characters from a string, either use a
pointer to unsigned char to access the string or use an explicit cast
to unsigned char on the char value. In theory, the former method is
more reliable (it is not perfectly clear what happens with byte values
that cannot be represented as char values), in practice both work.

The way the string was initialised simply does not matter in the context
of this discussion. Well, if it was initialised with a string literal
containing only characters from the basic source character set, no cast
is needed, because all the character values are guaranteed to be positive.

Dan Pop
DESY Zeuthen, RZ group
Email: Removed)
Reply With Quote

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
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
Re: for(size_t a=begin();a!=end();++a){} Chris \( Val \) C++ 2 07-14-2003 06:31 AM
Re: size_t ... standards Howard Hinnant C++ 5 06-30-2003 07:22 PM
Re: size_t ... standards Howard Hinnant C++ 0 06-29-2003 05:45 PM