question

Shervan360-6172 avatar image
0 Votes"
Shervan360-6172 asked WayneAKing-0228 edited

Dereferencing NULL pointer in C - Visual Studio 2019

Hello,

How can I remove the warning?

Severity Code Description Project File Line Suppression State
Warning C6011 Dereferencing NULL pointer 'dArray+r'. C_Project02 C:\Users\My\source\repos\C_Project02\C_Project02\C_Project0e2.c 20


     int n; scanf_s("%d", &n);
     int** dArray = calloc(n, sizeof(int*));
     for (int r = 0; r < n; r++)
     {
         *(dArray + r) = calloc((r + 1), sizeof(int));
     }


c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

After calloc completes successfully, dArray points to the first element of an array of n int*, each of which is initialized to 0. As coded, the compiler sees the dereference operator being applied to a value it knows to be 0. The compiler's logic is a little short-sighted since it fails to realize that this is on the left of the assignment operator.

If you code the statement as the equivalent dArray[r] = .... possibly the compiler would recognized that you are really not dereferencing the value of the r-th pointer but merely replacing it.

If you replace the initialization of dArray with malloc, I wonder if the compiler would complain about dereferencing an indeterminate value.

1 Vote 1 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered WayneAKing-0228 edited

In the description of the warning in the doc at the
link provided by RLWA32:

"This warning indicates that your code dereferences a
potentially null pointer."

the key word is "potentially". If calloc should fail
then the pointer will be NULL, if it succeeds it
won't be NULL. That will only be known at run time,
and could be different at different times under
varying circumstances. So the compiler has no way
to tell what will happen at run time, but it
can tell what might happen and is warning you
of the possibility.

As the doc also suggests, if you add code to verify
that calloc has not failed then the compiler will
suppress that warning. For example:

 int n; scanf_s("%d", &n);
 int** dArray = calloc(n, sizeof(int*));
 if(!dArray)
 {
     return -1;
 }
 for (int r = 0; r < n; r++)
 {
     *(dArray + r) = calloc((r + 1), sizeof(int));
 }

or

 int n; scanf_s("%d", &n);
 int** dArray = calloc(n, sizeof(int*));
 if(dArray)
 {
     for (int r = 0; r < n; r++)
     {
         *(dArray + r) = calloc((r + 1), sizeof(int));
     }
 }
  • Wayne


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 edited

I suggest you read the guidance about validating a pointer before you use it in the documentation for the warning message. See c6011. What would happen if calloc failed?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.