Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Valgrind--可能失去警告_C++_Valgrind - Fatal编程技术网

C++ Valgrind--可能失去警告

C++ Valgrind--可能失去警告,c++,valgrind,C++,Valgrind,我的代码如下: std::string myName = "BLABLABLA"; //check if there are illegal characters for (unsigned int i = 0; i < myName.length(); i++) { const char& c = myName[i]; if (!(isalnum(c) || (c == '_') || (c == '-'))) { return 0;

我的代码如下:

std::string myName = "BLABLABLA";

//check if there are illegal characters
for (unsigned int i = 0; i < myName.length(); i++)
{
    const char& c = myName[i];
    if (!(isalnum(c) || (c == '_') || (c == '-')))
    {
        return 0;
    }      

}
std::string myName=“blabla”;
//检查是否存在非法字符
for(无符号int i=0;i
这是valgrind在第“const char&c=myName[i];”行的输出

==17249==1个块中的51个字节可能在224的丢失记录116中丢失
==17249==at 0x4C2714E:运算符新(无符号长)(vg_替换_malloc.c:261)
==17249==by 0x602A498:std::string::_Rep::_S_create(无符号长、无符号长、,
std::allocator const&(在/usr/lib64/libstdc++.so.6.0.16中)
==17249==by 0x602A689:std::string::_M_mut(无符号长、无符号长、,
(in/usr/lib64/libstdc++.so.6.0.16)
==17249==by 0x602AFB5:std::string::_M_leak_hard()
/usr/lib64/libstdc++.so.6.0.16)
==17249==by 0x602B0A4:std::string::operator[](无符号长)(in/
/usr/lib64/libstdc++.so.6.0.16)

我看不出这有什么问题…

是的,这是一个可怕的COW实现! 您还可以强制使用常量(因此是非变异)重载,如下所示:

std::string const myName = "BLABLABLA";

//check if there are illegal characters
for (unsigned int i = 0; i < myName.length(); i++)
{
    const char& c = myName[i];
    if (!(isalnum(c) || (c == '_') || (c == '-')))
    {
        return 0;
    }      
}
std::string const myName=“BLABLABLA”;
//检查是否存在非法字符
for(无符号int i=0;i
或者(如果不想修改原始字符串类型):

std::string myName=“blabla”;
std::string const&cref=myName;
//检查是否存在非法字符
for(无符号int i=0;i
等等



,因为我知道我在某个地方写了一些关于它的东西。

魔鬼就在细节中。更具体地说,在GCC的
std::string
中。这不是你的错。这不是你的错。@KerrekSB gcc的字符串泄漏?@LuchianGrigore:是的。这是refcount业务…尝试在源代码的最顶端添加
#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
std::string const myName = "BLABLABLA";

//check if there are illegal characters
for (unsigned int i = 0; i < myName.length(); i++)
{
    const char& c = myName[i];
    if (!(isalnum(c) || (c == '_') || (c == '-')))
    {
        return 0;
    }      
}
std::string myName = "BLABLABLA";
std::string const &cref = myName;
//check if there are illegal characters
for (unsigned int i = 0; i < myName.length(); i++)
{
    const char& c = cref[i];
    if (!(isalnum(c) || (c == '_') || (c == '-')))
    {
        return 0;
    }      
}