Note that there are some explanatory texts on larger screens.

plurals
  1. POvalgrind & C++ : building std::string from character buffers
    primarykey
    data
    text
    <p>Simple C++ / valgrind question I'm hoping somebody can help out with.</p> <p>When running valgrind against the following code, I get two possible leaks related to std::string : </p> <pre><code>==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&lt;char&gt; const&amp;) (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&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;) (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&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.13) ==10325== by 0x40D0977: std::string::_Rep::_M_clone(std::allocator&lt;char&gt; const&amp;, 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&amp;) (in /usr/lib/libstdc++.so.6.0.13) ==10325== by 0x40D1D63: std::string::operator+=(std::string const&amp;) (in /usr/lib/libstdc++.so.6.0.13) ==10325== by 0x804879D: main (test.cc:11) </code></pre> <p>While it wouldn't be too much work to suppress them, and the code seems straightforward enough to me, I'd like to be sure I'm not missing something obvious here before I try to convince valgrind that I know what I'm doing.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string&gt; int main(int argc, char *argv[]) { char buffer[8192]; sprintf(buffer, "ABCD"); std::string str(buffer); std::string str2 = ""; str2 += str; exit(EXIT_SUCCESS); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload