Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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++ std::wstring::find()在g++;?_C++_G++_Valgrind_Wstring - Fatal编程技术网

C++ std::wstring::find()在g++;?

C++ std::wstring::find()在g++;?,c++,g++,valgrind,wstring,C++,G++,Valgrind,Wstring,此程序有什么问题: #include <string> int main() { std::wstring s = L"12345"; s.find(L"x"); return 0; } 那么Valgrind没有抱怨 我的环境: $ uname -a Linux dave-VirtualBox 4.15.0-101-generic #102-Ubuntu SMP Mon May 11 10:07:26 UTC 2020

此程序有什么问题:

#include <string>
int main()
{
    std::wstring s = L"12345";
    s.find(L"x");
    return 0;
}
那么Valgrind没有抱怨

我的环境:

$ uname -a
Linux dave-VirtualBox 4.15.0-101-generic #102-Ubuntu SMP Mon May 11 10:07:26 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ valgrind --version 
valgrind-3.13.0

不是一个真正的bug,但是旧的Valgrind版本可能不知道sse字符串操作


作为一种优化,str*和mem*函数可以执行8字节的读取,因为内存总是以8字节的增量分配。因此,这可能会读取超过字符串末尾的内容,但它永远不会读取未分配的内存。

可能是valgrind端的一个错误。我使用g++10.2和valgrind 3.16进行了测试,它运行正常。

请注意,当我将字符串大小减少一个字符时:--缩短字符数可能会调用
string
类的短字符串优化,因此不会分配内存。我使用g++10.2和valgrind 3.16进行了测试,。它没有显示错误。所以可能是因为旧版本的g++或valgrind上的错误。@Jaebum感谢您的提示。我在我的Ubuntu18上构建了Valgrind3.16.1。该版本的Valgrind报告没有错误。你能把你的信息作为答案发出去吗?我在我的Ubuntu18上建立了Valgrind3.16.1。该版本的Valgrind没有报告错误(g++版本未更改)。
#include <string>
int main()
{
    std::wstring s = L"1234"; // string is shorter by one character
    s.find(L"x");
    return 0;
}
#include <string>
int main()
{
    std::wstring s = L"12345";
    s.find(L"5"); // '5' is in the string
    return 0;
}
#include <string>
int main()
{
    std::string s = "12345"; // std::string instead of std::wstring
    s.find("x");
    return 0;
}
$ uname -a
Linux dave-VirtualBox 4.15.0-101-generic #102-Ubuntu SMP Mon May 11 10:07:26 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ valgrind --version 
valgrind-3.13.0