Re: c++ comp. argument passing and prefix operator
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Thu, 1 Sep 2005 10:29:14 -0400
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
.
- References:
- Prev by Date: Re: c++ comp. argument passing and prefix operator
- Next by Date: Re: start unix process from c++ win32 app
- Previous by thread: Re: c++ comp. argument passing and prefix operator
- Next by thread: RE: c++ comp. argument passing and prefix operator
- Index(es):
Relevant Pages
|