uint32_t * newArr = realloc( myStruct->arr, 2 * muStruct->Capacity * sizeof(myStruct->arr) )

if (newArr == null)
{
    free(myStruct->arr);
    return false;
}
else
{
    myStruct->arr = newArr;
    myStruct->Capacity *= 2;
    ...


Valgrind说:

Address 0x51f7c80 is 0 bytes after a block size of 80 alloc'd
    at 0x4C2BB78: realloc (vg_replace malloc.c:785)


这里发生了什么?是我的newArr ==空值吗?

最佳答案

该错误表明您正在超出数组范围的地方读(或写),如以下valgrind article concerning debugging of memory problems所述:


  sample2.c:从图2的输出中可以看到,对
  两个阵列中的元素513导致写入错误,读取错误和
  另一个写错误。消息地址0x40CA0224是0个字节
  分配的大小为512的块表示没有存储空间
  512字节数组的末尾。


这可能在程序中的其他位置。语句if (newArr == null) ... free(myStruct->arr)是正确的恕我直言。

关于c - Valgrind不喜欢我的realloc()吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42166038/

10-12 16:18