Re: <ctype.h> toLower()
From: Jumbo (pcrcutitout1000011_at_uko2.co.uk)
Date: 12/05/03
- Next message: André Pönitz: "Re: some general questions"
- Previous message: André Pönitz: "Re: some general questions"
- In reply to: Karl Heinz Buchegger: "Re: <ctype.h> toLower()"
- Next in thread: Karl Heinz Buchegger: "Re: <ctype.h> toLower()"
- Reply: Karl Heinz Buchegger: "Re: <ctype.h> toLower()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 5 Dec 2003 11:08:24 -0000
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3FD05C00.68EC91FB@gascad.at...
> Jumbo 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
>
> That depends on how the compiler arranges things.
> On a machine where the compiler is not able to put
> some protection on the actual characters it *is*
> possible to change a literal string. Now further if
> the compiler has figured out that the strings used in
> the lowerize call and the string used in the printf call
> are identical (that's trivial for a compiler), he may arrange
> that in both cases a pointr to the same memory area is used,
> where the compiler has placed the string "HI ALL". But then
> comes function lowerize and changes those characters. As a result
> printf uses this characters and prints "hello".
But you just said that string literals are constant so how can you assign
anything to a string literal?:
[quote 1]
Get a clue.
In this context a pointer to an array of *constant* characters is passed.
[end quote]
[quote 2]
Learn the language. There is an exception in the language which allows this.
But this doesn't change the fact that the pointer points to an array
of constant charcers.
[end quote]
[quote 3]
"HI" is not an array of char. It is an array of const char.
[end quote]
[quote 4]
And he is allowed to do so. But it is still a pointer to an array
of constant characters. The characters themself don't get non const
just because the pointer to them changes it's type. All the restrictions
still apply.
[end quote]
That's what you've been saying in your past messages.
So are you now trying to change your mind and say it's non-constant?
A string literal is not an l-value therefore it cannot be assigned to.
>
> That's an example of what undefined behaviour can mean. And
> you hopefully understand why this group usually makes a big
> fuzz about it. Anything can happen.
>
> (And yes: I have seen such things in action and it was no walk
> in the park to find the spot in the program where the real
> problem happend.)
>
> >
> > 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.
>
> May bad. I expressed in a wrong way.
> But the point is:
> The assignment may or may not throw an exception even in the case where
> a string literal is passed to it. Don't make the mistake that just because
> your windows machine throws an exception that it is the same on all
> other Non-Windows machines.
If a string literal is passed no assignment will take place as a string
literal is not an l-value.
>
> >
> > >
> > > Not really what one would expect.
> >
> > What would you expect?
>
> If I write
>
> printf( "HI ALL");
>
> I would expect the program to output "HI ALL" and nothing else.
That's what it does output.
>
> >
> > >
> > > > Obviously it's dependant of the programmers preferences as to which
> > style of
> > > > error codes he/she wishes to implement.
> > >
> > > The important thing is: Inside the function you have no way of
figuring
> > > out, if that function was used correctly. Therefore it is up to the
> > > caller to get things right.
> >
> > No if the caller gets it wrong the function handles it and does
nothing..
>
> On your Windows-machine. What you see is a property of Windows, not one
> of your program. On Non-Windows machines this is different.
So are you saying that on non-windows machines you can assign values to
string literals?
That doesn't sound to smart.
>
> >
> > This will do nothing except return -1:
> > lowerize("HI ALL", "HELLO");
> > ........
> > and the program will proceed as normal.
>
> On Windows machines, maybe. On Non-Windows machines maybe not.
> You have invoked undefined behaviour - anything can happen.
As above your trying to say that you can assign values to string literals.
This must be a non compliant compiler as you cannot assign to a non l-value.
Which compiler are you using ?
- Next message: André Pönitz: "Re: some general questions"
- Previous message: André Pönitz: "Re: some general questions"
- In reply to: Karl Heinz Buchegger: "Re: <ctype.h> toLower()"
- Next in thread: Karl Heinz Buchegger: "Re: <ctype.h> toLower()"
- Reply: Karl Heinz Buchegger: "Re: <ctype.h> toLower()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|