简体   繁体   English

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

[英]Using memcmp in C++ gives memory leak while running with Valgrind

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.

Amazingly, you could read the docs to see what the "invalid read" message means. 令人惊讶的是,您可以阅读文档以查看“无效阅读”消息的含义。 Eg 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处的内存的pcNewBeginPtr :该内存已被释放,未有效初始化以指向char缓冲区,指向已经退出的作用域中的局部变量等。

You might read some other questions: eg here . 您可能还会阅读其他一些问题:例如在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM