Simple C++/valgrind question J'espère que quelqu'un peut aider.valgrind & C++: construction de std :: string à partir des tampons de caractères
Lors de l'exécution valgrind contre le code suivant, je reçois deux fuites possibles liés à std :: string:
==10325== 17 bytes in 1 blocks are possibly lost in loss record 1 of 2
==10325== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==10325== by 0x40CFD05: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x40D0B10: ??? (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x40D0CF5: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x804872B: main (test.cc:9)
==10325==
==10325== 17 bytes in 1 blocks are possibly lost in loss record 2 of 2
==10325== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==10325== by 0x40CFD05: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x40D0977: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x40D17AC: std::string::reserve(unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x40D1C7F: std::string::append(std::string const&) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x40D1D63: std::string::operator+=(std::string const&) (in /usr/lib/libstdc++.so.6.0.13)
==10325== by 0x804879D: main (test.cc:11)
Bien qu'il ne serait pas trop de travail pour les supprimer, et le code semble assez simple pour moi, je voudrais être sûr que je ne manque pas quelque chose d'évident ici avant que j'essaie de convaincre valgrind que je sais ce que je fais.
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main(int argc, char *argv[])
{
char buffer[8192];
sprintf(buffer, "ABCD");
std::string str(buffer);
std::string str2 = "";
str2 += str;
exit(EXIT_SUCCESS);
}
+1 bonne prise! – wilhelmtell
En effet. Merci beaucoup! – cubic1271