【发布时间】:2021-07-08 13:51:59
【问题描述】:
我正在遵循 Microsoft 关于如何解决此警告的指南,但它似乎不起作用。
我添加了所有检查,但是,当我将“扩展”指针 tmp 复制到 ptr 并尝试使用它时,我收到另一个警告:
警告 C6200 索引“1”超出有效索引范围“0”到“0” 非堆栈缓冲区'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'.
*/
}
}
}
【问题讨论】:
-
您是否加入了
stdlib.h? -
@AndrésSanchez 您使用什么编译器和编译器标志?
-
OT:
int main(void) -
@david-ranieri 是的
已正确包含。 -
诊断信息似乎太笨了,无法识别 realloc。虽然自然
ptr[0] = 10; if (ptr != NULL)没有任何意义。如果你修复了这个错误,会有什么不同吗?
标签: c visual-studio malloc compiler-warnings realloc