Subject: Re: sbrk(0) dumps a core??
To: None <tech-userlevel@netbsd.org>
From: Christos Zoulas <christos@zoulas.com>
List: tech-userlevel
Date: 03/04/2001 08:31:32
In article <1epps2b.pbeak37yvmo4M@[10.0.12.137]>,
Emmanuel Dreyfus <p99dreyf@criens.u-psud.fr> wrote:
>Anyone can explain me why the following program dumps a core at mine?

Yes. brk interferes with malloc, and changing the break point after malloc
has been called, confuses malloc. stdio uses malloc.
Moving the brk call above the first printf (which makes the first call to
malloc) should fix the problem.

christos

>
>$ cat brk.c
>/* 
> * brk.c -- a brk() tester
> */
>#include <stdio.h>
>#include <unistd.h>
>
>int main (int argc, char** argv) {
>   void *end;
>   int res;
>   void *size = (void*)0x4000;
>
>   end = sbrk (0);
>   printf ("end=0x%lx\n", (long)end);
>
>   res = brk (size);
>   printf ("brk(0x%lx) returned %d\n", (long)size, res);
>
>   end = sbrk (0);
>   printf ("end=0x%lx\n", (long)end);
>
>   return 0;
>}
>$ gcc -Wall -Werror -ansi -o brk brk.c
>$ ./brk                        
>end=0x1840dbc
>Memory fault (core dumped) 
>
>-- 
>Emmanuel Dreyfus.  
>Vous avez deplacé la souris. 
>Windows NT doit maintenant redemarrer pour valider les modifications.
>p99dreyf@criens.u-psud.fr