하단에 유튜브 영상자료 있음.
#include <stdlib.h>
void *malloc (size_t size)
Successful :
- Retruns a pointer to a memory block of at least size bytes aligned to an 8-byte (x86) or 16-byte (x86-64) boundary.
- if size == 0, return NULL
Unsuccessful :
- Returns NULL(0) and set errno
void free(void *p)
other functions
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
> i.e., can only place allocated blocks in free memory
- Must align blocks so they satisfy all alignment requirements > 8-byte (x86) or 16-byte (x86-64) alignment on Linux boxes
- Can manipulate and modify only free memory > i.e., compaction is not allowed
Allocators are really interesting objects because they combine a trade-off of sort of running time sort of speed and space so it's kinda space and performance trade-off. you are trying to optimize both you want it to run as quickly as possible but you want to use you want it to use the VM in the heap as efficiently as possible.
It's very easy to make a really fast malloc that that has terrible memory utilization.
one obvious thing is that since blocks have to be aligned to some .. you know if there's sixteen by two lines then blocks have to start on 16 by boundaries and they have to be at least 16 bytes so if you were to request a payload of 2 bytes you'd have a lot of wasted bytes right that would sort of decrease the utilazation. This overhead is unavoidable but your job as a someone who writes a implements malloc is to try to keep that as small as possible.
동적 메모리 할당 카네기 대학 강의 영상.