我正在尝试将一个字符串 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/