# V604. Number of iterations in loop equals size of a pointer. Consider inspecting the expression.

The analyzer has detected a potential error in a construct that comprises a loop. The loop is odd because the number of iterations in it equals to the sizeof(pointer). It is highly probable that the number of iterations should correspond to the size of the array the pointer refers to.

Let's see how such an error might occur. This is how the program looked at first:

    char A[N];
    for (size_t i=0; i < sizeof(A); ++i)
      A[i] = 0;

Then the program code underwent some changes and the 'A' array has become a variable-sized array. The code has become incorrect:

    char *A = (char *)malloc(N);
    for (size_t i=0; i < sizeof(A); ++i)
      A[i] = 0;

Now the "sizeof(A)" expression returns the pointer size, not the array's size.

This is the correct code:

    char *A = (char *)malloc(N);
    for (size_t i=0; i < N; ++i)
      A[i] = 0;

This diagnostic is classified as:

CWE-682 CERT-ARR01-C

You can look at examples of errors detected by the V604 diagnostic.