Manual for int brk(void *end_data_segment); says: "brk() sets the end of the data segment to the value specified by end_data_segment"
On Success it returns 0 else -1.
But how to I get the init value of my break (like sbrk(0))?
best regards,
Manual for int brk(void *end_data_segment); says: "brk() sets the end of the data segment to the value specified by end_data_segment"
On Success it returns 0 else -1.
But how to I get the init value of my break (like sbrk(0))?
best regards,
As the manual states:
On success, brk() returns zero. On error, -1 is returned, and errno is set to ENOMEM
So there is no way to get the initial value through a call to brk
. Instead, as you noticed, you should use sbrk(0)
:
sbrk() increments the program's data space by increment bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break.
There is no reason to expect brk
to also provide this ability when it's already provided by sbrk(0)
. With that said, it may be more prudent to use use mmap
in general for your memory allocation needs, due to limitations on certain operating systems for brk
/sbrk
.