Re: <ctype.h> toLower()

From: Greg Comeau (comeau_at_panix.com)
Date: 12/05/03


Date: 5 Dec 2003 10:27:56 -0500

In article <1070617947.62507@news.minx.net.uk>,
Jumbo <pcrcutitout1000011@uko2.co.uk> wrote:
>"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
>news:3FD04B38.E3284974@gascad.at...
>> Jumbo wrote:
>> > I can't argue with that I better fix it straight away.
>> >
>> > int lowerize(char* dest, char* src){
>> > try{ dest[0] = src[0]; }
>> > catch(...){ return -1; }
>> > int _length = strlen(src);
>> > for(int i=0; i<=_length;i++)
>> > {
>> > dest[i] = tolower(src[i]);
>> > }
>> > return 1;
>> > }
>> >
>> > Returns -1 if error occurs.
>> > Returns 1 if no error occurs.
>> >
>>
>> Not true.
>> Assigning anything to a constant string literal brings up undefined
>> behaviour. Anything can happen and that includes: works as expected.
>>
>> Eg. the following can happen:
>>
>> ....
>>
>> int main()
>> {
>> lowerize( "HI ALL", "HELLO" );
>> printf( "HI ALL" );
>> printf( "\n" );
>> }
>>
>> And the program does not crash or throw an exception
>> but instead prints
>> hello
>
>No it doesn't .. It prints....
>HI ALL

He didn't say does, he said "can happen". And it can.
Granted, it may pring "HI ALL" for your machine.
But that was exactly Karl's point, since it's undefined behavior
it may do different things on different platforms, different
compilers, etc.

>It doesn't throw an exception, but it catches and handles one. The function
>returns -1 if it encounters an exception, otherwise it returns 1.

On which machine/platform/compiler do you know that it does that?

-- 
Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?


Relevant Pages

  • Re: fstream
    ... >|>int main ... >| I forget the exact dis/allowances here, but I'd just like to point ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: question about anonymous unions and classes
    ... > int i; ... union within a class. ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: writing to a string
    ... If you mean file sorting then you'd be using ... >guranteed to work on any platfrom in existence then you are no longer just ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: GCC question
    ... int is the default and where is the difference between no parameter ... type declaration whereas the new one is much to confusing. ... the prototype (without missing info) tends to cause it to be upfront. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.lang.c)
  • Re: [9fans] "Blocks" in C
    ... function pointers. ... scoping rules or something in that other language. ... World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90. ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.os.plan9)