0

I started to use Gcov to analyze my C programs. So I read, in the introduction of GCov documentation,this: "because gcov accumulates statistics by line (at the lowest resolution), it works best with a programming style that places only one statement on each line." GCov Documentation

Writing trivial programs, I noted problems in the calculation of number of times that a statement, contained in a "for" clause, is executed.

I'm going to show you two examples and the GCov output of these:

    -:    0:Source:example1.c
    -:    0:Graph:example1.gcno
    -:    0:Data:example1.gcda
    -:    0:Runs:1
    -:    0:Programs:1
    -:    1:#include <stdio.h>
    -:    2:#include <stdlib.h>
    -:    3:
    -:    4:
    1:    5:int main()
    -:    6:{
    1:    7:    int i = 0;
    -:    8:
    1:    9:    for(; i < 4; i++);
    -:   10:
    1:   11:    return 0;
    -:   12:}
    -:   13:

GCov counts the for clause as a unique element. It is not wrong?. As told by documentation GCov counts correctly the number of times that the statements in for clause are executed if I put these on one line, like in this example:

    -:    0:Source:example1.c
    -:    0:Graph:example1.gcno
    -:    0:Data:example1.gcda
    -:    0:Runs:1
    -:    0:Programs:1
    -:    1:
    -:    2:#include <stdio.h>
    -:    3:#include <stdlib.h>
    -:    4:
    -:    5:
    1:    6:int main()
    -:    7:{
    1:    8:    int i = 0;
    -:    9:
    6:   10:    for(;
    -:   11:        i < 4;
    4:   12:        i++);
    -:   13:
    1:   14:    return 0;
    -:   15:}
    -:   16:

We have that (i = 0) is executed 1 time, (i < 4) 5 times and (i++) 4 times. It is correct for me.

I have two questions:

1-I don't want to adapt my coding style in order to do statistics with GCov. Are there many tools or flags that transform my code in the correct format in order to get correct statistics with GCov?

2- Are there other constructs that have the same problem of "for" loop?

Thanks in advance for answers.

vnzstc
  • 19
  • 8

1 Answers1

0

There's nothing wrong the with the coding style used here.
The gcov annotated output shows it as executed once because it was optimized by the compiler, as the compiler sees the ; at the end of for it converts it into a single assignment. So, instead of traversing the loop in 5 steps it simply assigns the value and gets out of it.
You should try the same thing like this:

int i=0;
for(; i < 4; i++)
    printf("%d", i)

If try this one you will get right count of execution for each statement.
So I don't thing you have to adapt any particular coding convention, but avoid writing group of statements in a single(which no professional actually does anyway) line that's it.

Vikas Tawniya
  • 1,323
  • 1
  • 13
  • 22
  • 1
    Sorry for the late reply. But I'm not still convinced. GCov counts the "for" written in your way as unique statement. I receive 5 from the profiling. – vnzstc Jul 22 '17 at 16:23