我一定要瘋了,但我在Convert std::string to const char* or char*的例子之後,我在valgrind中得到了很多Invalid read of size 1
。我基本上試圖分割空白分隔的字符串,並將它們存儲到std::vector<const char*>
。我知道,因爲從c_str()
返回的指針可能隨時失效,您必須執行一個副本才能安全地使用數據。但是,自從valgrind抱怨之後,我似乎一定會失去一些東西。在絕對最低,我複製了頂部的答案,這樣做:將std :: string數據的深拷貝存儲到std :: vector中
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
std::vector<const char*> args;
std::string str = "hey";
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0'; // don't forget the terminating 0
args.push_back(writable);
delete[] writable;
for (size_t i = 0; i < args.size(); ++i)
cout << args[i] << "\n";
return 0;
}
下面的輸出,這導致從Valgrind的:
==5297== Invalid read of size 1
==5297== at 0x4C2BFC2: __GI_strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5297== by 0x400CFF: main (in /tmp/1411705143.17426/a.out)
==5297== Address 0x5c2a0a0 is 0 bytes inside a block of size 4 free'd
==5297== at 0x4C2A09C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5297== by 0x400CD9: main (in /tmp/1411705143.17426/a.out)
==5297==
==5297== Invalid read of size 1
==5297== at 0x4C2BFD4: __GI_strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5297== by 0x400CFF: main (in /tmp/1411705143.17426/a.out)
==5297== Address 0x5c2a0a1 is 1 bytes inside a block of size 4 free'd
==5297== at 0x4C2A09C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5297== by 0x400CD9: main (in /tmp/1411705143.17426/a.out)
==5297==
==5297== Invalid read of size 1
==5297== at 0x58E6DB8: _IO_default_xsputn (genops.c:480)
==5297== by 0x58E4D19: [email protected]@GLIBC_2.2.5 (fileops.c:1393)
==5297== by 0x58DA98C: fwrite (iofwrite.c:45)
==5297== by 0x4EC8A35: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (streambuf:451)
==5297== by 0x400D0F: main (in /tmp/1411705143.17426/a.out)
==5297== Address 0x5c2a0a0 is 0 bytes inside a block of size 4 free'd
==5297== at 0x4C2A09C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5297== by 0x400CD9: main (in /tmp/1411705143.17426/a.out)
==5297==
==5297== Invalid read of size 1
==5297== at 0x58E6DC7: _IO_default_xsputn (genops.c:479)
==5297== by 0x58E4D19: [email protected]@GLIBC_2.2.5 (fileops.c:1393)
==5297== by 0x58DA98C: fwrite (iofwrite.c:45)
==5297== by 0x4EC8A35: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (streambuf:451)
==5297== by 0x400D0F: main (in /tmp/1411705143.17426/a.out)
==5297== Address 0x5c2a0a2 is 2 bytes inside a block of size 4 free'd
==5297== at 0x4C2A09C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5297== by 0x400CD9: main (in /tmp/1411705143.17426/a.out)
==5297==
我懷疑,這種解決方案並不因爲工作刪除後,矢量內的數據指向垃圾。我也試過args.push_back(std::move(writable))
。什麼是解決方法?
爲什麼不直接存儲'std :: string'?爲什麼要讓自己頭疼存儲'char'數組副本? – Brandon 2014-09-26 04:36:58