Strange malloc Error

Bob Proulx bob@proulx.com
Mon Aug 29 01:38:00 GMT 2005


Mike McWilliam wrote:
>          The error is in C++ code. Essentially I am getting
> segmentation faults on cals tol malloc(). It's not all malloc calls.
> In fact I call malloc a lot in this program it's just this one.

Segmentation faults in malloc or free are usually an indication of a
buffer overflow problem somewhere else in your code. 

>         As a test I used a for loop to call malloc and free with
> increasing memory size. At a certain limit it crashes. So I figured
> its a memory limitation.

I strongly suspect that you have a buffer overrun in your program.
This question gets asked enough that it is a long standing FAQ.

  http://www.faqs.org/faqs/C-faq/faq/

  7.19: My program is crashing, apparently somewhere down inside malloc,
        but I can't see anything wrong with it.  Is there a bug in
        malloc()?

  A:    It is unfortunately very easy to corrupt malloc's internal data
        structures, and the resulting problems can be stubborn.  The
        most common source of problems is writing more to a malloc'ed
        region than it was allocated to hold; a particularly common bug
        is to malloc(strlen(s)) instead of strlen(s) + 1.  Other
        problems may involve using pointers to memory that has been
        freed, freeing pointers twice, freeing pointers not obtained
        from malloc, or trying to realloc a null pointer (see question
        7.30).

        See also questions 7.26, 16.8, and 18.2.

There are a variety of progams available to help with memory
problems.

  http://dmalloc.com/

  http://valgrind.org/

  http://perens.com/FreeSoftware/ElectricFence/

And many others...

Bob



More information about the Gcc-help mailing list