Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: $i=$i++

by ariels (Curate)
on Apr 30, 2002 at 08:17 UTC ( [id://163012]=note: print w/replies, xml ) Need Help??


in reply to $i=$i++

To expand on tye: What gcc does for "i=i++;" is conforming behaviour for ISO C. What any implementation does for it is conforming!

This sort of code (technically, modifying a variable more than once between sequence points) is specifically said by the standard to evoke undefined behaviour (see the comp.lang.c FAQ). In the comp.lang.c.* newsfroups, it is said that the compiler could compile code which causes daemons to fly out of your nose (aka nasal daemons)! It could also choose to reformat your hard disks except on Friday the 13th, return the value 17, or do anything else.

Is this "broken"? Not really. By not specifying evaluation order more precisely, the standard allows the compiler a lot more latitude in compiling efficient code. This, at the price of disallowing some essentially useless code. (Would you be happier if the standard said "i=i++; is guaranteed to set the value of i to -29"? Would you use it?)

Note that all this applies to code generation, not to parsing. That "i++ - ++i + i" is parsed as "(i++ - ++i) + i" has nothing to do with the code emitted by the compiler. Parsing has no idea of "the same" object; that is part of the semantics of C, not its syntax. But not everything illegal is a syntax error!

Perl has not had such a rigourous standardization. Furthermore, only one implementation exists at any time (assuming we decide 5.004, 5.005, 5.00503, 5.6.0, 5.6.1, and the rest are all different). So it is very tempting to apply the "test and see" engineering technique.

I'd join tye in recommending you avoid this. First, because it isn't documented behaviour. Unfortunately, Perl has loads of behaviour that couldn't really be said to be "documented" and still gets used. The thing is, these other properties are actually useful. The syntax for them evokes a specific interpretation. Can you really give a good argument for compiling "(++A)+(B++)" (legal code, if A and B are different objects) as any of the following codes, rather than any of the others?

  1. LOAD R1, A INCR R1 LOAD R2, B ADD R3, R1, R2 INCR R2 STORE A, R1 STORE B, R2
  2. LOAD R1, A INCR R1 STORE A, R1 LOAD R2, B ADD R3, R1, R2 INCR R2 STORE B, R2
Note that if A and B happen to be the same object, you get different results...

Replies are listed 'Best First'.
Re: Re: $i=$i++
by Anonymous Monk on Apr 30, 2002 at 13:08 UTC
    Hi, thanks for the extensive reply. It was just what I was after. I am fully aware that this is code that shouldn't be used, I was just curious why it differed as addition shouldn't care about left/right side.
    /jon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://163012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found