Subject: Re: sbrk(0) dumps a core??
To: Emmanuel Dreyfus <>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-userlevel
Date: 03/04/2001 02:34:12
>> Whoa Nelly!  You've just set the break to 0x4000; if you have any
>> data segment above that, it just went poof.
> Ok, [...].  [T]his still crashes, why?
[irrelevant lines snipped -dM]

>         end = sbrk (0);
>         printf ("end=0x%lx\n", (long)end);
>                 newend = (void*)((long)end + 4000);
>         end = sbrk (0);
>         printf ("end=0x%lx\n", (long)end);
>         res = brk (newend);
>         printf ("brk(0x%lx) returned %d\n", (long)newend, res);

(Well, you *should* be using %p and casting to void *, rather than %lx
and casting to long, but I don't think NetBSD currently runs on a
machine where the difference will bite you, and for machine-specific
debugging code this isn't really an issue...except that getting into
bad habits tends to lead to using them even when it *does* matter.)

> Ouput:
> end=0x1840e68
> new_end=0x1841e08
> end=0x1852000
> Memory fault (core dumped) 

> Is it that printf caused some allocation, hence a end move, and that
> I'm calling brk with and adress which is lower than end?

Yes.  Look at the value returned by sbrk(0) after the printf and before
you brk(): it was 0x1840e68 and then it moved to 1852000.  You then
brk() it back down to 1841e08, and all the memory between there and
1852000 goes poof.

Why are you adding a value to the break and using brk, anyway, rather
than just using sbrk?  It sure looks to me as though what you're trying
to do is a perfect match to sbrk()'s semantics.

					der Mouse

		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B