Author Topic: A proper, fleshed out explanation for brk()?  (Read 9968 times)

Offline MediocreVeg1

  • Jr. Member
  • *
  • Posts: 26
A proper, fleshed out explanation for brk()?
« on: January 18, 2021, 05:37:37 AM »
It's pretty sad that after the basic IO system calls, there is very little information regarding the rest. For this reason, I had a lot of trouble finding out about sys_brk(), and if anyone here knows fully about it, or has seen a site which will answer most/all of the questions I'm asking, please share.

1. What exactly is brk()? The only thing I really know is that it extends some breakpoint, but that's about it. So basically, what is the breakpoint, and how does brk() relate to it?
2. How does dynamic memory allocation work then? Does it reserve the specified bytes in the heap/stack? (Speaking of which, how do we even use the heap in assembly?) I found an example of brk() on github here https://gist.github.com/nikAizuddin/f4132721126257ec4345 and it was pretty useful, but it still doesn't explain how exactly we can access the memory we have reserved. Does it require some other syscall?
3. Are there any other uses of brk()?

Offline MediocreVeg1

  • Jr. Member
  • *
  • Posts: 26
Re: A proper, fleshed out explanation for brk()?
« Reply #1 on: January 18, 2021, 06:58:17 PM »
Ok, I found out how it works. I'll give a very brief answer to each question I've made:
1. Basically, brk() just reserves space between the heap and the stack and returns an address pointing towards the address of this reserved space.
2. Mostly mentioned in my answer to 1 as well. About accessing, I was being a total idiot and just made a variable that pointed towards the address returned by brk.
3. Still not sure about this yet.

However, I learnt that for dynamic memory allocation, mmap might be a better choice. I can't seem to find any good examples and explanations on that though. If anyone has a point to add, I probably won't respond for a few hours since it's pretty late at night here.