Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting valgrind error Invalid write of size 4

Tags:

gcc

valgrind

I was recently trying to track down some bugs in a program I am working on using valgrind, and one of the errors I got was:

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

the offending line # 414 reads

amplitudes__[points_read] = 0x0;

and amplitudes__ is defined earlier as

uint32_t * amplitudes__ = (uint32_t* ) amplitudes;

Now obviously a uint32_t is 4 bytes long, so this is the write size, but could someone tell me why it's invalid ?

like image 848
camelccc Avatar asked May 07 '12 10:05

camelccc


People also ask

What does invalid read of size mean in Valgrind?

An Invalid read means that the memory location that the process was trying to read is outside of the memory addresses that are available to the process. size 8 means that the process was trying to read 8 bytes. On 64-bit platforms this could be a pointer, but also for example a long int.

What does invalid write mean in Valgrind?

“Invalid write” means that our program tries to write data in a memory zone where it shouldn't. But Valgrind tells you way more than that. It first tells you the size of the written data, which is 1 bytes, and corresponds to the size of a character.

How do you show line numbers in Valgrind?

Look for function names and line numbersIf you compile your program with the -g flag, Valgrind will show you the function names and line numbers where errors occur.

What is Valgrind Linux?

Valgrind (/ˈvælɡrɪnd/) is a programming tool for memory debugging, memory leak detection, and profiling. Valgrind. Original author(s)


1 Answers

points_read is most likely out of bounds, you're writing past (or before) the memory you allocated for amplitudes.

like image 114
Mat Avatar answered Sep 29 '22 22:09

Mat