Re: (i++ * i++)

thp_at_cs.ucr.edu
Date: 04/16/04


Date: Fri, 16 Apr 2004 04:49:29 +0000 (UTC)

Nils Petter Vaskinn <no@spam.for.me.invalid> wrote:
: On Wed, 24 Mar 2004 15:04:32 +0000, Christopher Benson-Manica wrote:
:
:> Thomas Matthews <Thomas_MatthewsSpitsOnSpamBots@sbcglobal.net> spoke thus:
[...]
:>
:> It seems that you're responding to OP's broken code. Either that or
:> you saw an i++ in Nils' code where where there in fact was none.
:> Either that or you believe the expression i may result in an increment
:> operation ;)
:
: Either that or he meant that since the intent of the OP's code isn't
: obvious my solution won't nessessarily match it. But my example can be
: trivially modified to account for any conceivable intent of the original
: code:
:
: // if the intent was 5*6 leaving i as 7
: int i = 5;
: std::cout << (i * i++) << std::endl;
: i++;

Sorry, "i*i++" begets undefined behavior, the former value of the
modified object is used for purposes other than to determine the new
value of that object. (I don't like it either!)

: ...
:
: // if the intent was 5*6 leaving i as 6
: int i = 5;
: std::cout << (i * i++) << std::endl;

Still undefined behavior.

: ...
:
: // if the intent was 5*5 leaving i as 6
: int i = 5;
: std::cout << (i * ++i) << std::endl;

Still undefined behavior.

: ...
: // if the intent was undefined behaviour
: int *i;
: std::cout << *i << std::endl;

Right, because i is uninitialized.

C89 6.3 and its vestiges in C99 and C++ are unfortunate design flaw in
C/C++. A significant fraction of the practicing C/C++ programmers are
unaware of this loophole for optimizers and/or don't understand it.
Someday that lack of awareness will hurt someone.

Tom Payne



Relevant Pages

  • Re: int a=printf("%d ",a);
    ... [Assuming that pesky 'int' weren't there...] ... >>meaning and intent of that phrase is somewhat unclear to me). ... No undefined behavior there at all. ...
    (comp.lang.c)
  • Re: program is not crashing, after 10 iteration
    ... your code results in undefined behavior. ... Actually, it occurs much earlier, during the first iteration. ... ptr invokes UB. ... On many systems (where int has no trap representations and the ...
    (comp.std.c)
  • Re: I want my segmentation fault!
    ... no occurrences of free and a lot of routines returning pointers to ... int length_of_list; ... This invokes undefined behavior because ml1 has been freed. ... memory is really available for reuse, I'd be glad to know about it. ...
    (comp.lang.c.moderated)
  • Re: pointer to const int
    ... int main ... ..and in fact the former causes undefined behavior. ... it's not an explicit requirement stated normatively in the standard ... terminating new-line character is implementation-defined. ...
    (comp.lang.c)
  • Re: How to send dynamic vector?
    ... It invokes undefined behavior and you ... main should return int. ... Since you don't pass rank to this function, ...
    (comp.lang.c)