110 likes | 161 Views
Interlude: Memory API. Joonmin Jeon(jmjeon@cslab.snu.ac.kr) School of Computer Science and Engineering Seoul National University. Types Of Memory. low. stack. heap. heap. code, data, bss. high. Stack Managed by compiler implicitly Allocated when call function
E N D
Interlude: Memory API Joonmin Jeon(jmjeon@cslab.snu.ac.kr) School of Computer Science and Engineering Seoul National University
Types Of Memory low stack heap heap code, data, bss high • Stack • Managed by compiler implicitly • Allocated when call function • Deallocated when return function • Accessing directly • Heap • Managed by programmer explicitly • Allocated when call allocate API(e.g. malloc) • Dellocated when call deallocate API(e.g. free) • Long-live variables • Access via pointer in stack
The malloc() Call #include <stdlib.h> ... void *malloc(size_t size); 1 double *d = (double *) malloc(sizeof(double)); Common API for allocate heap memory Prototypes of function Return NULL if API call fails Return address for new allocated memory if API call success For access other parts of function, save returned address into stack variable likes following
The free() call int *x = malloc(10 * sizeof(int)); ... free(x); Common API for deallocate heap memory Usage of function
Common Errors (1/4) 1 char *src = "hello"; 2 char *dst; 3 strcpy(dst, src); 1 char *src = "hello"; 2 char *dst = (char *) malloc(strlen(src)); 3 strcpy(dst, src); • Forgetting to allocating memory • Segmentation Fault occurs • Not allocating enough memory • Sometimes works properly but, it should harmful
Common Errors (2/4) 1 char *src = (char *) malloc(10 * sizeof(char)); 2 printf("%s", src); 1 while( flags ) { 2 src = (int *)malloc(sizeof(int)); 3 } • Forgetting to Initialize Allocated Memory • No one knows what might be in there, what occurs • Forgetting to Free Memory • Memory Leak Occurs • Even Garbage Collection support language, it might occurs • Short-running program doesn’t matter, but long-running program does make critical issue.
Common Errors (3/4) 1 char *src = (char *) malloc(10 * sizeof(char)); 2 free(src); 3 src = "hello"; 1 char *src = (char *) malloc(10 * sizeof(char)); 2 free(src); 3 free(src); • Freeing memory Before you are done with it • Can crashing program, or overwrite valid memory • Called Dangling pointer • Freeing memory repeatedly • Usually crashing occurs • Memory-allocation Library might get confused and do sort of weird thing
Common Errors (4/4) 1 free(INT_MAX); • Calling free() Incorrectly • free() with incorrect memory address • Like freeing memory repeatedly, it usually occurs crashing.
Underlying OS Support #include <unistd.h> void *brk(const void *addr); #include <sys/mman.h> void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); • brk(), sbrk() system call • Change break: end of heap • Never call this system call directly • mmap() system call • Create anonymous memory region • Treated like heap and managed as such
Other Calls void *calloc(size_t count, size_t size); void *realloc(void *ptr, size_t size); • calloc() • Like malloc() but fill zeros for allocated memory • Prevent some error • Function Prototype • realloc() • Allocate memory filled with data of pre-allocated memory • Function Prototype
Summary • Types of Memory • stack – managed by compiler • heap – managed by programmer • APIs for manage heap • malloc() – allocate heap memory • calloc() – allocate heap memory with zero filled • realloc() – resize allocated heap memory • free() – deallocate heap memory • Common Errors • Usual mistake while managing heap • Underlying OS Support • brk(), sbrk() • mmap()