Re: *str++ is undefined?
From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 03/19/05
- Next message: Flash Gordon: "Re: Can't compile this with Cygwin!"
- Previous message: jacob navia: "Re: set and reset the least significant bit of a address"
- In reply to: Michael B Allen: "Re: *str++ is undefined?"
- Next in thread: Michael B Allen: "Re: *str++ is undefined?"
- Reply: Michael B Allen: "Re: *str++ is undefined?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 19 Mar 2005 01:01:38 -0800
Michael B Allen wrote:
> On Sat, 19 Mar 2005 01:30:23 -0500, CBFalconer wrote:
>>>
>>> *str++ = tolower(*str);
>>>
>>> that gcc is complaining about:
>>>
>>> warning: operation on `str' may be undefined
>>
>> No. You are using an undefined value, because the ++ may actually take
>> effect either before or after the other use of the same variable.
>
> That's stupid. The right side of the expression must be evaluated before
> an assignment can be made.
> ...
That's true. But the act of actual assignment consists of one and only
one action: storing the [suitable converted] result of the 'tolower'
call in the destination object of type 'char'. But the process of
_locating_ the destination object is not a part of the act of
assignment. The compiler is free to determine the destination object
_before_ the right-hand side is evaluated, and then perform the actual
assignment after the right hand side is evaluated. For example, the
above expression can be evaluated in accordance with the following schedule
// Determine the destination
char* dst = str++;
// Prepare the operand
char op = *str;
// Call the function
char res = tolower(op);
// Perform the assignment
*dst = res;
Or it can be evaluated in accordance with a different schedule
// Prepare the operand
char op = *str;
// Call the function
char res = tolower(op);
// Determine the destination
char* dst = str++;
// Perform the assignment
*dst = res;
Note, that both schedules are completely legal, i.e. they don't violate
any sequencing requirements imposed by the language specification. In
both cases the actual assignment is performed at the very end (i.e. "the
right side of the expression is evaluated before the assignment" as you
said). But the outcomes are completely different.
(One can easily come up with yet more possible schedules with yet more
possible outcomes).
-- Best regards, Andrey Tarasevich
- Next message: Flash Gordon: "Re: Can't compile this with Cygwin!"
- Previous message: jacob navia: "Re: set and reset the least significant bit of a address"
- In reply to: Michael B Allen: "Re: *str++ is undefined?"
- Next in thread: Michael B Allen: "Re: *str++ is undefined?"
- Reply: Michael B Allen: "Re: *str++ is undefined?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|