Domanda

I compiled the following program with "gcc -fprofile-arcs -ftest-coverage test.c":

int main() {
  int a = 1;
  int b = 1;
  a && b;
}

After running the program, an invocation of "gcov -bc test.c" reports that 50% of 4 branches were taken at least once. Why does gcov say the program has four branches, instead of two? After running the following program, gcov correctly reports that 50% of 2 branches were taken at least once:

int main() {
  int a = 1;
  if (a)
    a = 0;
  else
    a = 1;
}
È stato utile?

Soluzione

Apparently gcov is considering a&&b to be the following:

if(a) { // branch 1
    if(b) { // branch 2
        1; 
    } else { // branch 3
        0;
    }
} else { // branch 4
    0;
}

Though I'm fairly certain the actual CPU instructions will translate to a single branch.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top