From owner-freebsd-questions@FreeBSD.ORG Sat Sep 20 11:16:41 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BAC55106564A for ; Sat, 20 Sep 2008 11:16:41 +0000 (UTC) (envelope-from trashy_bumper@yahoo.com) Received: from web110515.mail.gq1.yahoo.com (web110515.mail.gq1.yahoo.com [67.195.8.120]) by mx1.freebsd.org (Postfix) with SMTP id 8B2678FC1A for ; Sat, 20 Sep 2008 11:16:41 +0000 (UTC) (envelope-from trashy_bumper@yahoo.com) Received: (qmail 66286 invoked by uid 60001); 20 Sep 2008 11:16:41 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:MIME-Version:Content-Type:Message-ID; b=kuY+i7WGlqFvCR4f7SQNsiQ5KPICbH6APIdhoLu5elqHbH7Y1OjLdJ+dfmyw8Ygz7d4j1+Yffs/hwN6Eo9PVxW1afnCAsayALPqzBrh4xpbUchNPd6LTOtXJH+PbAy4dcMiwoL/KygMWN8JFmq1e9ewCff2DBKiZnO9ftd5PL0U=; X-YMail-OSG: RkUL2hgVM1lNhJBAtIMvBlYkxeroKJzK9ICXyG6UJ8xNtuqZLn2WhyPhIuyITQgzrUmS_SmAPNChoF2ng56pSTroJxjBGgb5jUAAMPtdsDn2lMc6vzTWLp4VPkWbUPZvOd9aYA-- Received: from [77.122.205.244] by web110515.mail.gq1.yahoo.com via HTTP; Sat, 20 Sep 2008 04:16:40 PDT X-Mailer: YahooMailWebService/0.7.218.2 Date: Sat, 20 Sep 2008 04:16:40 -0700 (PDT) From: Nash Nipples To: freebsd-questions@freebsd.org MIME-Version: 1.0 Message-ID: <14392.66160.qm@web110515.mail.gq1.yahoo.com> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: Re: Segmentation fault when free X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: trashy_bumper@yahoo.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 20 Sep 2008 11:16:41 -0000 > I checked again, up to the this problematic free(), > functions return newly allocated strings properly: >=20 > char *f( ) > { > char *newstr =3D NULL; > : > newstr =3D (char *) malloc(p - sp + 1); > if (newstr =3D=3D NULL) > return NULL; > : > return newstr; > } >=20 > Can a yet not executed wrong free() elsewhere cause a > problem of this nature? >=20 > Best regards > Unga >=20 i'm sorry i just dont really cant see a problem with free() can you please explain it in a more simple way? it really starts to exceed my screen height #include #include #include char *a; char *b; char *c; char *abd =3D "Hi, im a string 1\0"; char *bbd =3D "Hey, im a string 2\0"; char *f(char *d){ =A0 char *newstr =3D NULL; char *p, *sp; =A0 sp =3D malloc(strlen (d)); memcpy(sp, d, strlen(d)); =A0 p =3D strchr(sp, 44); =A0 newstr =3D (char *) malloc(p - sp + 1); =A0 if (newstr =3D=3D NULL){ =A0=A0=A0 newstr =3D malloc(1); =A0=A0=A0 *newstr =3D '\0'; =A0=A0=A0 return newstr;=A0 /* you really dont want to return NULL to strle= n() */ =A0 } =A0 *p =3D '\0'; =A0 memcpy(newstr, sp, sizeof(char) * (p - sp)); =A0 return newstr; } char *f1(void){ =A0 char *ab; =A0 ab =3D f(abd); =A0 printf("f1(): %s\n", ab); =A0 return ab; } char *f2(void){ =A0 char *bb; =A0 bb =3D f(bbd); =A0 printf("f2(): %s\n", bb); =A0 return bb; } int main(void) { while (1) { a =3D f1(); /* malloc and send a string */ b =3D f2(); /* malloc and send a string */ c =3D (char *) malloc(strlen(a) + strlen(b) + 1); c[0] =3D '\0'; strcat(c, a); strcat(c, b); printf("main(): %s\n", c); free(a); free(b); free(c); } } =0A=0A=0A