Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH] unmetafy Re: $var not expanded in ${x?$var}



On Thu, Feb 22, 2024 at 8:23 AM Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
>
> > +             fwrite(unmetafy(dupstring(str), &num), num, 1, file);
>
> Being no C expert, I wonder if it's safe (portable) to set and
> use num in the same call like that. Is it guaranteed to be done
> in the right order?

In C, function arguments are evaluated in unspecified order. The
behavior of the fwrite() call above depends on the evaluation order of
its arguments, and is therefore unspecified.

Evaluation can even be interleaved. Consider:

    a(b(), c(d()))

Here, it is possible that functions will be invoked in this order: d,
b, c. Basically, function arguments are evaluated before the function
is invoked (obviously), but other than that there are no evaluation
order guarantees. Here's another example of unspecified behavior:

    *f() = g();

Here, f() and g() can be evaluated in any order.

There is a related gotcha where the same object is accessed more than
once between sequence points and at least one of these accesses is a
write. Like this:

    int a = 0;
    int b = ++a + a;

Or like this:

    foo(++a, a);

This is undefined behavior, not just unspecified.

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author