Why would I get a bus error or segmentation fault when calling free() normally?

Posted by chucknelson on Stack Overflow See other posts from Stack Overflow or by chucknelson
Published on 2010-04-18T15:03:05Z Indexed on 2010/04/18 15:13 UTC
Read the original article Hit count: 204

Filed under:
|
|

I have a very simple test program, running on Solaris 5.8:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *paths;
    paths = getenv("PATH");
    printf("Paths: %s\n", paths);

    free(paths); // this causes a bus error
    return 0;
}

If I don't call free() at the end, it displays the message fine and exits. If I include the free() call, it crashes with a bus error. I've had other calls to free(), in other programs, cause segmentation faults as well.

Even if I allocate the memory for *paths myself, free() will cause a bus error. Is there some reason trying to free up the memory is causing a crash?

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory-management