Segmentation fault when free

Unga unga888 at yahoo.com
Fri Sep 19 16:17:19 UTC 2008


Hi all

I'm running FreeBSD 7 on i386. I have a C program compiled with gcc 4.2.1 20070719.

Logically my program is:

char *a;
char *b;
char *c;

while (cond)
{
 a = f1(); /* malloc() and send a string */
 b = f2(); /* malloc() and send a string */

 c = (char *) malloc(strlen(a) + strlen(b) + 1);
 c[0] = '\0';

 strcat(c, a);
 strcat(c, b);

 free(a);
 free(b);
}

When it executes free(b), my program exits with Segmentation fault: 11. The free(a) executes well.

The problem is with free(b). Even swap free(b) first and free(a) next, it still crashes at free(b).

If I comment out free() lines, further down the program, first few characters of one string get dropped when executes a completely unrelated line.

How could I bit more narrow down the problem? 

Many thanks in advance.

Kind regards
Unga








      


More information about the freebsd-questions mailing list