How debug segfault at realloc()?

Dereferencing invalid pointers invokes undefined behaviour which means that anything could happen, including the behaviour that you're observing. If I had to guess, I'd say that either: your pointer really is invalid (it wasn't obtained from malloc or you've already free d it, or you've modified it accidentally you've overflowed a buffer somewhere, and trashed malloc s behind-the-scenes metadata I would suggest running your application in something like Valgrind, which is designed to help track down memory issues.

Dereferencing invalid pointers invokes undefined behaviour, which means that anything could happen, including the behaviour that you're observing. If I had to guess, I'd say that either: your pointer really is invalid (it wasn't obtained from malloc, or you've already freed it, or you've modified it accidentally. You've overflowed a buffer somewhere, and trashed malloc's behind-the-scenes metadata.

I would suggest running your application in something like Valgrind, which is designed to help track down memory issues.

It seems like I wrote over the end of the malloc'ed memory, and that later somehow made realloc crash as well. I didn't know malloc has metadata behind the scenes and presumably realloc then interacts with that..? I'm not crashing anymore, so that seems to have done it – Blub Nov 14 at 17:40 @Blub: Yes, a common implementation of malloc is to allocate more space than you request, and then place metadata outside the "visible" region. – Oli Charlesworth Nov 14 at 17:42.

There are a few possibilities: The memory is valid, but not obtained via malloc or realloc (string literal, global, static, automatic memory, memory obtained via mmap() or via shm*) the memory has been obtained via malloc, but the pointer is not the exact pointer to the beginning of the object (the one you got from malloc() You might have skipped initial white, or your pointer points into a structure or 2d array, etc. You have corrupted your memory by using an incorrect offset or pointer anywhere in the program In your case choosing between the options (1,2) and 3 above is easy: you can visually inspect how the "cs->body" pointer got its value. If is was really obtained via malloc et.Al, only option 3 is open. Without using valgrind, the only way to find spurious overwrites is to (temporally) rollback the latest changes in the source.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions