==24303== Uninitialised value was created by a heap allocation

1k views Asked by At

Valgrind complains about the line with new (the first one):

     #ifndef MAXI_BUF_SIZE
     #define MAXI_BUF_SIZE 2*8192
     #endif

if (lg_maxi_buf == NULL)
{
      lg_maxi_buf = new unsigned char[MAXI_BUF_SIZE] ;
}
else
{
      delete [] lg_maxi_buf ;
      lg_maxi_buf = NULL ;
      lg_maxi_buf = new unsigned char[MAXI_BUF_SIZE] ;
}

I wanted to explain that the field lg_maxi_buf is part of class Audio_decoder and it is declared like this:

unsigned char *lg_maxi_buf;

in the constructor of the class Audio_decoder lg_maxi_buf is initialized in this way:

lg_maxi_buf = NULL;

Thanks for your answers. I think I will have to use smart pointers, but my knowledge on them is very poor.

1

There are 1 answers

0
johan d On

you could simplify your if-else in

delete [] lg_maxi_buf ;
lg_maxi_buf = new unsigned char[MAXI_BUF_SIZE] ;

The delete operator ignores NULL parameters (you avoid many ifs that way. I personally don't affect NULL prior a new, but I understand if you prefer.

To initialize this bloc of memory, just add parenthesis:

lg_maxi_buf = new unsigned char[MAXI_BUF_SIZE]();