Dynamic Memory
Notes
- We will be using some c functions for this
- They are routines for manipulating the heap directly, but that is messy
-
void * malloc(size_t size)- Attempts to allocate size bytes on the heap
- Returns the address if successful
- returns null (0) if not.
-
void free(void *ptr)- Returns the memory pointed to by pointer to the heap
- Both of these are partial truths, but they will do for us.
- These are lower level versions of
newanddeletein c++ - To use them we need
extern malloc extern free - The rest is simple
- store the number of bytes you want in rdi
- call malloc
- rax will hold either 0 if the call failed or the address of memory
- Address this memory as we have been.
- When you are finished
- Store the address malloc returned in rdi
- call free