Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Sep 2008 09:17:17 -0700 (PDT)
From:      Unga <unga888@yahoo.com>
To:        freebsd-questions@freebsd.org
Subject:   Segmentation fault when free
Message-ID:  <37804.54110.qm@web57007.mail.re3.yahoo.com>

next in thread | raw e-mail | index | archive | help
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








      



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?37804.54110.qm>