The message Address 0x51f60a0 is not stack'd, malloc'd or (recently) free usually just part of the larger Valgrind error message.
These Valgrind error messages usually look something like this:
Invalid read of size 4 at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9) by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9) by 0x40B07FF4: read_png_image__FP8QImageIO (kernel/qpngio.cpp:326) by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621) Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
or
Invalid read of size 8 at 0x40060E: free_adj_list (main.c:9) by 0x400844: main (main.c:65) Address 0x4c1d170 is 16 bytes inside a block of size 24 free'd at 0x4A04D72: free (vg_replace_malloc.c:325) by 0x400609: free_adj_list (main.c:8) by 0x400844: main (main.c:65)
How to read these error messages
The first part of the message indicates what went wrong ("Invalid reading of size 4" means that you tried to read from a memory address that you should not access), followed by backtracking where the error occurred.
The backtrace is followed by the memory address you were trying to access. Valgrind makes an assumption about what you could keep in mind by looking to see if there is an address:
- outside the portion of memory that you have access to (therefore, your program has overflowed the buffer). An example message would be
Address 0x1002772ac is 4 bytes after a block of size 12 alloc'd - inside a memory block that was previously free (therefore, your program used memory after it was freed ); Example:
Address 0x4c1d170 is 16 bytes inside a block of size 24 free'd
Then, these messages are followed by a second return trace, which indicates where you allocated or freed up the mentioned memory.
But the message Address 0x51f60a0 is not stack'd, malloc'd or (recently) free'd means that Valgrind could not guess what you wanted to do. You tried to access memory at 0x51f60a0, but this address was not recently freed and is not located near any other part of the memory you allocated. Thus, you can be fairly sure that the error in this case is not a buffer overflow and is not a usage error after release.
How to debug errors like this
Thus, we can assume that 0x51f60a0 is a more or less "random" memory address. I can think of two possible reasons:
- the pointer that you dereferenced contains some uninitialized value; in this case, you should also get the
Use of uninitialised value error message from Valgrind - you dereferenced a value that was not intended at all as a pointer - for example. the value may actually be the result of some unrelated calculations in your program, and somehow you wrote that value to the pointer you used later
In addition, of course, there is a possibility that the error is actually a buffer overflow or subsequent use, but Valgrind could not detect it.
How to debug this error in your program
I think one way to narrow down the problem would be to run the application in Valgrind using GDB to find out which memory access is exactly causing the error (is this node Bad? Is node[length-1] bad? Is node[0] bad ?). Then find out how the bad value first came about.