Re: c++ comp. argument passing and prefix operator

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Guru <Guru@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> I tried the following 4 line of code and answer is what I never
> expected.I ran the code using C++ compiler(MS-Studio .net 03) also in
> Unix env.In fact the on Unix i got the correct (or expected answer).
> -->
> void fn(int, int);
> main()
> {
> int a = 5;
> fn(a++, ++a);

This statement exhibits undefined behavior. Any result from this is a
correct one, since the C++ standard does not place _any_ expectations on
the outcome. Your personal expectations do not count, really.

Specifically, this code runs afoul of C++ standard 5/4:

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions, and the order in
which side effects take place, is unspecified. Between the previous and
next sequence point a scalar object shall have its stored value modified
at most once by the evaluation of an expression. Furthermore, the prior
value shall be accessed only to determine the value to be stored. The
requirements of this paragraph shall be met for each allowable ordering
of the subexpressions of a full expression; otherwise the behavior is
undefined.

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: Linux Sys Calls - int 80h
    ... load the parameters in the required registers and make the "int 0x80" ... Clearly, this stuff is all "processor specific"...also, UNIX doesn't ... are "processor specific" and UNIX was designed with "portability" in ...
    (alt.lang.asm)
  • Re: Big integer newbie problem
    ... asking Unix questions in a C newsgroup. ... to know a fair amount about C and a reasonable number of ... unsigned long long int physmem; ...
    (comp.lang.c)
  • istream read not reading everything
    ... I'm having trouble using istream to read in a file in its entirety on ... through a unix pipe to the program everything works ... int isready ... int timecount = 0; ...
    (comp.lang.cpp)
  • Problems with compilation
    ... under both Windows & UNIX. ... gc.c: In function `int main': ... gc.c:81: warning: assignment to `clock_t' from `double' ...
    (comp.programming)
  • Re: C arithmetic question.
    ... long int a = -58; ... Is this a bug in the compiler or a problem with the C standard? ... changing his expectations to match the standard. ...
    (comp.lang.c)