You are here:

C/free(), and malloc error

Advertisement


Question
(Using GCC 3.2.3 under RH Enterprise Linux)

I have just re-written a function. All of a sudden, my program now sometimes crashes in a completely different function when I try to free up some allocated memory. On debugging, I get the following error message:

Program received signal SIGSEGV, Segmentation fault.
0x008a730e in malloc_consolidate () from /lib/tls/libc.so.6

I know that this is related to my new function, because if I remove it, the program does not crash. I have used mtrace() to deduce that there are no memory leaks. I have found on the internet that some errors can be caused by freeing the same memory twice, but having double-checked my new function, I have not done this.

Can you offer any suggestions for what might be causing this error?

Answer
So, according to you your program is correct and computer may be doing something wrong!!?
Without looking at your code, how can I give any suggestion!?

You have already checked that, you are not freeing a NULL pointer.
You have already checked that, you are not trying to free second time.
The only thing left is to check the addresses.
Check the address when you did malloc().
Check the address before doing free().
If both are not same, then that can be the cause of crash. That means you have changed the pointer at some place in the new function.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.