在使用Valgrind运行时,在C ++中使用memcmp会导致内存泄漏

时间:2020-12-28 09:00:31

I am using memcmp for comparing the char pointer to empty string as:

我使用memcmp将char指针与空字符串进行比较:

if((0 == memcmp("", pcNewBeginPtr, 1))){
    // do some stuff
}

I am able to compare this, but while running through Valgrind, I get this error message:

我可以比较这个,但在运行Valgrind时,我收到此错误消息:

Invalid read of size 1 at this line.

1 个解决方案

#1


4  

Amazingly, you could read the docs to see what the "invalid read" message means. E.g. you don't legitimately have read access to the memory at pcNewBeginPtr: it's already been freed, wasn't validly initialised to point at a char buffer, points to a local variable in a scope that's already exited etc....

令人惊讶的是,您可以阅读文档以查看“无效读取”消息的含义。例如。你没有合法地拥有对pcNewBeginPtr内存的读访问权:它已经被释放,没有被有效地初始化为指向char缓冲区,指向已经退出的范围中的局部变量等....

You might read some other questions: e.g. here.

您可能会阅读其他一些问题:例如这里。

#1


4  

Amazingly, you could read the docs to see what the "invalid read" message means. E.g. you don't legitimately have read access to the memory at pcNewBeginPtr: it's already been freed, wasn't validly initialised to point at a char buffer, points to a local variable in a scope that's already exited etc....

令人惊讶的是,您可以阅读文档以查看“无效读取”消息的含义。例如。你没有合法地拥有对pcNewBeginPtr内存的读访问权:它已经被释放,没有被有效地初始化为指向char缓冲区,指向已经退出的范围中的局部变量等....

You might read some other questions: e.g. here.

您可能会阅读其他一些问题:例如这里。