this is my code:
void foo(int num) {
int *pArr = (int *)malloc(num * sizeof(int));
// allocate array of 'sale' structs for each region
for (int i = 0; pArr != NULL && i < num; i++) {
pArr[i] = 1;
}
}
int main() {
int num = 36;
foo(num);
}
The expression pArr[i] = 1;
gives a C6386 warning
Warning C6386 Buffer overrun while writing to 'pArr': the writable size is 'num*sizeof(int)' bytes, but '8' bytes might be written.
This is very weird since the number of iterations of the for
loop, AND the size of the array in the head are both dependant on num
so an overrun can't actually occur ever.
And then there is a detailed explanation:
i
may equal 1
pArr
may beNULL
(Continue this loop)
Invalid write topArr
, (outside its writable range)
But of course this is a visual studio mistake, pArr
cannot be NULL
, as this is a condition for entering the loop.
How can I clean this warning?
Thank you all