Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 20 Sep 2008 04:16:40 -0700 (PDT)
From:      Nash Nipples <trashy_bumper@yahoo.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: Segmentation fault when free
Message-ID:  <14392.66160.qm@web110515.mail.gq1.yahoo.com>

next in thread | raw e-mail | index | archive | help

> 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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      



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