C resolution function

I am in the process of writing a non-destructive transformation procedure in C, and I just realized that (at least in Visual C ++) functions are resolved in reverse order.

So:

main() { char* psString[] = { "Hello", "World", "World2", "World3" }; printf("%s - %s - %s - %s\n", H2A_N(psString[0]), H2A_N(psString[1]), H2A_N(psString[2]), H2A_0(psString[3])); return 1; }; 

My code is not repetitive and works in only one thread, so I was going to use a static array of characters to store the results.

My plan was to have a function H2A_0 that wrote the output to the beginning of the buffer and left the next address for subsequent calls to H2A_N.

I understand that there are some flaws in this approach, but I have to apply this conversion procedure to a lot of existing code, so the simpler the better (I don't want to interfere with freeing up memory).

My question is:

  • Is it part of the C standard for solving functions (when they are passed as arguments to other functions) in reverse order.
  • Is there a better way to do this?
+4
source share
3 answers

There is no specific order of parameter estimation. The compiler can decide which order to evaluate as you like.

The latest version of the C11 standard says this: ยง6.5.2.2 / 10:

After evaluating the function designation, there is a sequence point and the actual arguments, but before the actual call. Each evaluation in the calling function (including other function calls) that otherwise does not have a specific sequence before or after the execution of the body of the called function is indefinitely ordered with respect to the execution of the called function.

If you want to force a certain evaluation order, you need to get the expressions out of the function call and save them in local variables.

 int param1 = foo(...); int param2 = bar(...); DoSomething(param1, param2); 

Is there a better way to do this?

Most likely. The presence of functions returns values, and also applies side effects to a fixed length of statically allocated buffers, which is generally not considered to be best practice. However, I would not want to tell you how to solve your problem, because I do not know enough about the problem.

+7
source

Is it part of the C standard for resolving functions (when they are passed as arguments to other functions) in reverse order.

The argument of functions is evaluated as an expression; and as such, it is determined by the implementation, as in x = f() + g() . If f() will be called before g() depends on the compiler to compiler, check the sequence .

Some compilers, such as icc , may receive a warning about this:

note # 981: operands are evaluated in an unspecified order

+1
source

You must first check. There is no specific order of permission. Most compilers seem to resolve left to right.

-2
source

Source: https://habr.com/ru/post/1483979/


All Articles