0

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,

user1735225
  • 113
  • 10

1 Answers1

0

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.

Alex DiCarlo
  • 4,851
  • 18
  • 34
  • Hm... wikipedia says "The current Mac OS X implementation of sbrk is an emulation, and has a maximum allocation of 4 Megabytes.[1] When this limit is reached, -1 is returned and the errno is not set." So should I use sbrk only for init? – user1735225 Jan 13 '13 at 19:48
  • It would help if you described your reasons to use `sbrk` at all. By default, I'd answer that you shouldn't use it. – Anton Kovalenko Jan 13 '13 at 19:50
  • 1
    @user1735225 I do not know the specifics of the Mac OS X implementation and it is beyond the scope of this specific question, and I suggest you ask another so someone more qualified can answer it. However as Anton mentioned, I would recommend not using `sbrk` at all, prefer `mmap` for your memory allocation needs. Additionally, if the above clarifies your original question, consider [accepting it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Alex DiCarlo Jan 13 '13 at 19:52
  • it is not about using, it is about implementing :D. I will implementing them as stupid as they are descript... thanks for your help. – user1735225 Jan 13 '13 at 20:22