I am following Microsoft's guidelines on how to work around this warning but it doesn't seem to work.
I add all the checks but, when I copy the 'expanded' pointer tmp
to ptr
and try to use it, I get another warning:
Warning C6200 Index '1' is out of valid index range '0' to '0' for non-stack buffer 'ptr'
void main(void)
{
int* ptr, * tmp;//I start with two pointers
ptr = (int*)malloc(1 * sizeof(int));
ptr[0] = 10;
if (ptr != NULL)//make sure ptr is not null.
{
tmp = (int*)realloc(ptr, 5 * sizeof(int));//expand tmp up to 5
if (tmp != NULL)//make sure it went well.
{
ptr = tmp;
ptr[1] = 20;
/*
Warning C6200 Index '1' is out of valid index range '0' to '0' for non-stack buffer 'ptr'.
*/
}
}
}