Originally Posted by mannoj
I did the debug process. I watched all variables, every thing is allright but why it crashes in malloc_consolidate()after perticular numbers of iterations?
There are lots of possibilities. The most common one is that some part of your program wrote to memory that didn't belong to it and that memory happened to be part of the structures malloc uses to keep track of which memory is free vs. allocated. When malloc later tries to consolidate newly freed memory with previously freed memory, it runs into those corrupted data structures and crashes.
If your program is small enough to post the entire source code, I expect it is simple enough for any experienced programmer to spot the error, so post it and someone will tell you what the error is.
If it isn't small enough to post and you need to find the problem by debugging, that can be quite a challenge for a beginner. An expert would debug a build that includes debug info for malloc itself as well as debug info for the program. Then you can see which memory location in malloc's internal structures was clobbered to cause the crash. Then you can restart and make the debugger stop on every modification to that address and you can see which modification is not legitimate.
There are also tools you can use to instrument your program and/or run it under a cpu simulator rather than directly in the CPU, so it can watch for and report many cases of routines writing to memory they don't own. We (where I work) mainly investigate such issues in Windows with Purify, so I forget the names of similar tools available free in Linux.
If you just want to study your own source code looking for the bug, look for places it keeps/uses a pointer to an object that could have been deleted and for places it might write beyond the end of an array.