Error interpretation valgrind Invalid size 4 record

I recently tried to track down some errors in a program that I am working on using valgrind, and one of the errors I received:

==6866== Invalid write of size 4 ==6866== at 0x40C9E2: superneuron::read(_IO_FILE*) (superneuron.cc:414) 

breaking line # 414 reads

 amplitudes__[points_read] = 0x0; 

and amplitudes__ are previously defined as

 uint32_t * amplitudes__ = (uint32_t* ) amplitudes; 

Now it’s obvious that uint32_t is 4 bytes long, so this is the size of the record, but can anyone tell me why it is not valid?

+6
source share
2 answers

points_read , most likely, is out of bounds, you write the past (or earlier) memory allocated for amplitudes .

+4
source

A typical mistake that new programmers make to get this warning is:

 struct a *many_a; many_a = malloc(sizeof *many_a * size + 1); 

and then try reading or writing to memory at the address "size":

 many_a[size] = ...; 

Here the distribution should be:

 many_a = malloc(sizeof *many_a * (size + 1)); 
+2
source

Source: https://habr.com/ru/post/915078/


All Articles