Segmentation fault when free

Nash Nipples trashy_bumper at
Sun Sep 21 12:57:08 UTC 2008

> > can someone please explain to me what happens to the
> allocated memory
> > called within a function assigned to its local pointer
> after this function
> > ends
> Ok - let's see if I get this right:
> - the allocated memory
> - called within a function
> - assigned to a local pointer
> Any malloc'ed memory is application global accessible.
> Assigning a pointer to 
> a variable doesn't allocate memory (the compiler and
> runtime libraries 
> already setup storage for the variable, at declaration
> time). So, I have no 
> idea what you mean with the "called within a
> function" part.
> -- 
> Mel
thanks for making it even more clear to me.
actually what i meant was this:

void function(void){
  char *p;
  p = malloc(1);
int main(void){
  while (1){
    /* in the end of this function function()
     * the memory is still allocated
     * even when the only pointer who knows its address
     * does not longer exist
     * which is why we have to free() the memory
     * during the application runtime
     * to avoid it from growing to ridiculous size
but even if you kill -SEGV `pgrep this` (Segmentation fault (core dumped) the memory is getting freed anyway (presumably by the glorious kernel). which you can see dynamicly by typing top in the console.

in other words segmentation fault when free() is not a scary thing here. it is a matter of style and the way to find own errors. or maybe reading warnings if you compile with the flags -ansi -pedantic 

oh and by the way:

>     char *
>     function(void)
>     {
>         char buffer[100];
>         return buffer;
>     }

that is an easier approach because you get warned on passing an address to a local variable


More information about the freebsd-questions mailing list