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 == null 情况吗?
最佳答案
该错误表明您在某处读取(或写入)超出了数组的范围,如 valgrind article concerning debugging of memory problems 中所述。 :
sample2.c: As you can see from the output in Figure Two, references to element 513 in the two arrays cause a write error, a read error, and another write error. The message Address 0x40CA0224 is 0 bytes after a block of size 512 alloc'd indicates that there is no storage beyond the end of the array of 512 bytes.
这可能是您程序中的其他地方。语句 if (newArr == null) ... free(myStruct->arr)
是正确的恕我直言。
关于c - Valgrind 不喜欢我的 realloc()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42166038/