c++gccg++stack-based

gcc app runs irregularly but clang works fine


I have written a simple stack based virtual machine you can view it's source at https://github.com/radinParsaei/VM when I compile it with clang it works well but if it compiles with GCC in bytecodes I have coded like pop() / pop() it work irregular and it returns stack[stack.size() - 2] / stack[stack.size() - 1] i fix this with writing that code like Value a = pop(); Value b = pop(); return a / b; anyone has better idea?


Solution

  • As @IlCapitano says, your second pop() may be called first:

    There is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression a() + b() + c() is parsed as (a() + b()) + c() due to left-to-right associativity of operator+, but the function call to c may be evaluated first, last, or between a() or b() at run time

    The key: the function call to c may be evaluated first, last, or between a() or b() at run time.

    In response to comment:

    order of evaluation is unspecified (with some exceptions). The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again.

    So it depends on the compiler. Each does it differently.