Re: [xml] [offtopic] evaluation order of logical operators



Volker Grabsch wrote:
 for(int i=0, e=xpo->nodesetval->nodeNr; i<e; ++i) {
   xmlChar* p_val = xmlGetProp(xpo->nodesetval->nodeTab[i], "id");
   if(p_val && 0==strcmp("4", p_val)) {

Please note that in C the order of execution of

    p_val

and

    0==strcmp("4", p_val)

is not specified! 

For a short time you put me off my stride. I do this all the time.
Then I found the following statement in ISO C99 [1]:

  6.5.13 Logical AND operator

  4  Unlike the bitwise binary & operator, the && operator guarantees
     left-to-right evaluation; there is a sequence point after the
     evaluation of the ïrst operand. If the ïrst operand compares equal
     to 0, the second operand is not evaluated.

Some compilers could generate code that
checks the second condition first, which could lead to a
segmentation fault or similar.

This may be true. Fortunately I newer saw such a compiler.


Cheers, Andy

[1] http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]