4

I was wondering if any one could explain in relatively simple terms, how to allocate heap memory?

I'm using x64 assembler (intel syntax) on a Linux machine. Up until now I have relied on making a call to the C function malloc, but I'm interest in the proper way.

Gary
  • 13,303
  • 18
  • 49
  • 71
tinyhippo
  • 93
  • 1
  • 5
  • 1
    The days when you could just claim any physical memory you wanted are long gone (think DOS). AFAIK dynamically allocated memory is allocated for a program by the operating system, so I'd guess you *do* have to make some sort of system call. What OS are you using? – stakx - no longer contributing Sep 10 '15 at 19:10
  • Unless you are a very advanced programmer - I mean Master Yoda level - "proper way" to allocate memory in C **is** using the malloc(), not assembly. It's not a trival operation, just check out Doug Lea's article about his malloc implementation at http://g.oswego.edu/dl/html/malloc.html . Not to mention the low portability issue. – Alex Byrth Sep 10 '15 at 19:24
  • 1
    There are multiple ways. Read about `sbrk()` for the old-fashioned way to allocate heap memory and read about `mmap()` for the modern way which is more complex to get right but also more flexible. – fuz Sep 11 '15 at 07:51
  • For posteriority: https://stackoverflow.com/questions/22586532/assembly-x86-brk-call-use/44876873#44876873 – Luiz Martins Nov 19 '20 at 23:17

1 Answers1

2

There are syscall lists available online that give you parameters to call directly (instead of the C function). Example list: http://blog.rchapman.org/post/36801038863/linux-system-call-table-for-x86-64 Example usage: http://callumscode.com/blog/3

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
  • 2
    Specifically, the syscall you want is `brk` or `sbrk`. See http://stackoverflow.com/questions/6988487/what-does-brk-system-call-do . Real life implementations of malloc() allocate large chunks at a time, and then manage those. – Seva Alekseyev Sep 10 '15 at 19:18