Use of delete[] but bytes are definitely lost, why?
<p>To explain my issue, this is minimalist main.cc:</p>

<pre><code>#include "./main.h"

int main(int argc, char *argv[]) {
    char *buffer = new char[12];
    char *output = new char[12];
    FILE *input  = fopen("file.test", "r");

    while ( read_stdin(buffer, 12, input, output) ) {
        // Operations on output
        // (...)
    }

    fclose(input);
    delete[] output; output = 0;
    delete[] buffer; buffer = 0;

    return 0;
}
</code></pre>

<p>And main.h:</p>

<pre><code>#include &lt;cstdio&gt;
#include &lt;cstring&gt;

inline bool read_stdin(char *tmp_buffer, const size_t &amp;len, FILE *input, char *&amp;output) {
    output = fgets(tmp_buffer, len, input);
    if ( output != NULL ) {
        char *lf = strchr(output, '\n');
        if ( lf != NULL ) {
            *lf = '\0';
        }
        return true;
    }
    return false;
}
</code></pre>

<p>The function read_stdin() can read from STDIN, it explains its name.</p>

<p>Well, all work as expected, but valgrind tells me things like:</p>

<pre><code>==6915== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6915==    at 0x4C29527: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6915==    by 0x4008A2: main (main.cc:6)
</code></pre>

<p>I compile as <code>g++ -O0 -g main.cc -o test</code></p>

<p>I understand that these 12 bytes are "output", but why is there some bytes lost? I use of delete[], even if there is nothing from STDIN or input, output will be NULL right?</p>

<p>I misunderstand why there are still these 12 bytes, where am I wrong?</p>

<p>Thank you in advance :)</p>

<p><strong>EDIT</strong></p>

<p>Thanks to Vaughn Cato, Dietmar Kühl and Richard J. Ross III, I changed lines:</p>

<pre><code>output = fgets(tmp_buffer, len, input);
    if ( output != NULL ) {
</code></pre>

<p>to</p>

<pre><code>if ( fgets(output, len, input) != NULL ) {
</code></pre>
