Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #164436

Re: Is the output of this program compiler dependent?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Is the output of this program compiler dependent?
Date 2022-01-17 08:46 +0100
Organization A noiseless patient Spider
Message-ID <ss36s7$ask$1@dont-email.me> (permalink)
References <ss2nci$a2k$1@gioia.aioe.org> <ss2okf$i0l$1@gioia.aioe.org>

Show all headers | View raw


On 17/01/2022 04:39, Satan wrote:
> Sorry this was posted on the wrong newsgroup so I am posting it here!
> 
> 
> On 17/01/2022 03:00, Satan wrote:
>> I read somewhere that the parameters of functions are evaluated right >>
>> to left but it all depends on the compiler. Therefore, does this mean
>> that the output of this program is "undefined" meaning there is no
>> definite answer!
>>


Your conclusion is correct, but the reasoning is slightly wrong.

First, the order of evaluation of parameters to a function is
"indeterminately sequenced".  (As is the function identifier itself,
which could be a function pointer.)

So if you have "foo(A, B)", then the compiler must either evaluate "A"
before evaluating "B", or evaluate "B" before evaluating "A".  It can
pick either.  It doesn't even have to have a fixed order in the code -
it could swap the orders between different calls.  But logically it has
to do one completely before doing the other.  (As always, the compiler
can generate mixtures of the evaluations as long as the results match
the logical requirements.)

Some more limited compilers have fixed evaluation order of function
parameters.  More optimising compilers will choose whatever works best
at the time.

But it is not the unspecified ordering of evaluation of the arguments
that is the problem in your code.  It is a matter of "sequence points".
 These can be thought of as barriers in the code - logically, when the
code hits a sequence point, everything before it must be finished and
then you can start on everything after it.  The end of a statement is a
sequence point - so for "i++; i--;" the increment has to be completed
before the decrement can be done, and everything works out safely.

There is no sequence point between evaluating the arguments of a function.

Between sequence points, you are limited in what you can do as the state
of the program has to be consistent.  For any given object, you can
either write to it once, or read from it (as often as you like) - you
can't do both, and you can't write more than once.  This makes sense if
you think about it.

("++i" is treated as "(i += 1)" or "(i = i + 1)", and there is a
sequence pointer at the assignment operator.)


So the trouble with "fun(--i, i++);" is /not/ a matter of which function
argument is evaluated first.  It is that you are mixing multiple reads
and writes to "i" without a clear ordering - and that is undefined
behaviour.

Note that "undefined behaviour" does not just mean there is no definite
answer - it means that /anything/ can happen.  It's not just a matter of
getting different integers printed when using different compilers
(though that's the most likely outcome) - maybe you'll get nothing at
all, or a crash, or demons will fly out of your nose.  Trying to execute
undefined behaviour is a really bad thing!




>> #include <stdio.h>
>>
>> void fun(int, int);
>>
>> int main()
>> {
>>       int i = 5;
>>       fun(--i, i++);
>>       fun(++i, i--);
>>       printf("From Main: %d\n", i++);
>>
>>       return 0;
>> }
>>
>> void fun(int x, int y)
>> {
>>       printf("From Function: %d  %d\n", x++, y--);
>> }
>>
> 
> 
> 
> 

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Is the output of this program compiler dependent? Satan <satan@dot.com> - 2022-01-17 03:39 +0000
  Re: Is the output of this program compiler dependent? Richard Damon <Richard@Damon-Family.org> - 2022-01-16 23:19 -0500
  Re: Is the output of this program compiler dependent? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-16 23:24 -0500
  Re: Is the output of this program compiler dependent? David Brown <david.brown@hesbynett.no> - 2022-01-17 08:46 +0100
    Re: Is the output of this program compiler dependent? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-17 12:36 +0000
      Re: Is the output of this program compiler dependent? David Brown <david.brown@hesbynett.no> - 2022-01-17 15:26 +0100
        Re: Is the output of this program compiler dependent? Öö Tiib <ootiib@hot.ee> - 2022-01-17 08:04 -0800
        Re: Is the output of this program compiler dependent? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-17 15:51 -0500
    Re: Is the output of this program compiler dependent? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-17 15:52 -0500

csiph-web