Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 468 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sequence point from function call?

#1
This is yet another sequence-point question, but a rather simple one:

#include <stdio.h>
void f(int p, int) {
printf("p: %d\n", p);
}

int g(int* p) {
*p = 42;
return 0;
}

int main() {
int p = 0;
f(p, g(&p));
return 0;
}

Is this undefined behaviour? Or does the call to `g(&p)` act as a sequence point?
Reply

#2
No. It doesn't invoke *undefined* behavior. It is just *unspecified*, as the order in which the function arguments are evaluated is unspecified in the Standard. So the output could be `0` or `42` depending on the evaluation order decided by your compiler.
Reply

#3
The behavior of the program is unspecified since we don't know the order of evaluation of the function arguments, from the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdf">draft C++ standard</a> `1.9` *Program execution paragraph 3*:

>Certain other aspects and operations of the abstract machine are described in this International Standard **as unspecified (for example, order of evaluation of arguments to a function).** Where possible, this International Standard defines a set of allowable behaviors. [...]

and all side effects from the arguments are sequenced before the function is entered, from section `5.2.2` *Function call paragraph 8*:

>[ Note: The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another. **All side effects of argument expression evaluations are sequenced before the function is entered** (see 1.9). —end note ]

As for `C` both points are covered in the <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf">C99 draft standard</a> in section `6.5.2.2` *Function calls paragraph 10*:

>The order of evaluation of the function designator, the actual arguments, and
subexpressions within the actual arguments is unspecified, but there is a sequence point
before the actual call.

So in both `C` and `C++` you can end up with either `f(0,0)` or `f(42,0)`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through