c++ - Valgrind 提示 string.append(string)

标签 c++ string append valgrind

我正在尝试将一个字符串 append 到另一个字符串。我声明了两个全局字符串变量 -

string grid_filename = "grids/";
string rest;

然后我有一个获取命令行参数的函数。每当用户在命令行参数中输入文件名时,它应该存储在 rest 中,然后将 rest append 到 grid_filename。

else if(strcmp(temp.substr(0,16).c_str(), "--grid-filename=") == 0) {
    rest = temp.substr(16,strlen(temp.c_str())-16);
    grid_filename.append(rest);   //line 74!
}

现在每当我运行我的代码时,valgrind 都会给我这个错误 -

==5602==  Address 0x45fdc30 is 0 bytes after a block of size 32 alloc'd
==5602==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==5602==    by 0x43039F7: std::string::_Rep::_S_create(unsigned int, unsigned int,     std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==5602==    by 0x4304C77: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==5602==    by 0x4304DA6: std::string::reserve(unsigned int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==5602==    by 0x43053E9: std::string::append(std::string const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.14)
==5602==    by 0x804D5AE: get_command_line_args(int, char**) (main.cpp:74)
==5602==    by 0x804F138: main (main.cpp:244)

我打印出字符串的两个地址,但它们都不匹配 valgrind 所说的 0 字节。我在这里缺少什么?

我相信这会导致我的第二个错误,因为我将 grid_filename 传递给另一个通过 tcp 连接发送字符串的函数。 Valgrind 告诉我

==5660== Syscall param socketcall.send(msg) points to unaddressable byte(s)
==5660==    at 0x404A9B1: send (socket.S:64)
==5660==    by 0x804F7C8: main (main.cpp:364)

谁能给我解释一下问题出在哪里?任何帮助,将不胜感激。如果需要,我可以提供更多有关代码的信息。

最佳答案

关于您的第一个错误:我们在 valgrind 中有误报。检查documentation抑制这些,特别是如果它们没有指向您的代码(并且您已经检查它们实际上不会导致问题)

关于c++ - Valgrind 提示 string.append(string),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9910837/

相关文章:

python - 尝试编写 For-While 循环,但不起作用

C++ 构造函数继承无法正常工作

c++ - 在 C++ 中使用没有合作伙伴 Class/Cpp 文件的头文件是否可行

c++ - 从线程池工作线程使用时 GetQueuedCompletionStatus 的奇怪行为

c++ - 将 void* 转换为 std::string

c++ - 为什么我不需要取消引用 char 指针来输出 c 风格的字符串?

c# - 循环 LINQ 查询并将结果追加到 DataTable

Python:如何根据给定文件夹的内容创建名称列表?

c++ - 链接列表释放内存 - 段错误

python - 如何在 Pandas 中将数据格式更改为 '%Y%m%d'?