1 / 11

Interlude: Memory API

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

Download Presentation

Interlude: Memory API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interlude: Memory API Joonmin Jeon(jmjeon@cslab.snu.ac.kr) School of Computer Science and Engineering Seoul National University

  2. 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

  3. 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

  4. The free() call int *x = malloc(10 * sizeof(int)); ... free(x); Common API for deallocate heap memory Usage of function

  5. 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

  6. 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.

  7. 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

  8. Common Errors (4/4) 1 free(INT_MAX); • Calling free() Incorrectly • free() with incorrect memory address • Like freeing memory repeatedly, it usually occurs crashing.

  9. 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

  10. 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

  11. 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()

More Related