c - Valgrind 不喜欢我的 realloc()?

标签 c memory valgrind heap-memory realloc

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/

相关文章:

文本文件解析器的 Java 字符串内存性能改进

c - valgrind 测试内存管理出错?

c - 无效的内存读取 - Valgrind

c - TCP write() 将 char* 打印到自身而不是仅在第一个套接字连接上缓冲,但在第二个连接上工作正常

c - 如何将字节内存从内核模块映射到用户空间应用程序?

c - 尝试使用函数基于另一个列表创建排序列表

c - C 中的 Instruments 未检测到内存泄漏

perl - 如何将 valgrind 输出转换为 XML?

c++ - C/C++ 拼图 : To print values from 1. .15 15..1 带有一个 for 循环

c - C 编程语言有运行时吗?