Understanding Memory Allocation and Management in Operating Systems

14 memory api operating system three easy pieces n.w
1 / 18
Embed
Share

Learn about memory allocation functions like malloc and free in C programming, along with the importance of properly allocating and freeing memory to prevent issues like memory leaks and segmentation faults. Explore examples and best practices for efficient memory management.

  • Memory Allocation
  • Operating Systems
  • malloc
  • free
  • C Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 14. Memory API Operating System: Three Easy Pieces 1 Youjip Won

  2. Memory API: malloc() #include <stdlib.h> void* malloc(size_t size) Allocate a memory region on the heap. Argument size_t size : size of the memory block(in bytes) size_t is an unsigned integer type. Return Success : a void type pointer to the memory block allocated by malloc Fail : a null pointer 2 Youjip Won

  3. sizeof() Routines and macros are utilized for size in malloc instead typing in a number directly. Two types of results of sizeof with variables The actual size of x is known at run-time. int *x = malloc(10 * sizeof(int)); printf( %d\n , sizeof(x)); 4 The actual size of x is known at compile-time. int x[10]; printf( %d\n , sizeof(x)); 40 3 Youjip Won

  4. Memory API: free() #include <stdlib.h> void free(void* ptr) Free a memory region allocated by a call to malloc. Argument void *ptr : a pointer to a memory block allocated with malloc Return none 4 Youjip Won

  5. Memory Allocating 2KB pointer heap (free) stack int *pi; // local variable *pi 16KB Address Space 2KB allocated allocated allocated allocated 2KB + 4 2KB + 8 2KB + 12 pi = (int *)malloc(sizeof(int)* 4); (free) 2KB *pi 16KB Address Space 5 Youjip Won

  6. Memory Freeing 2KB freed freed freed freed 2KB + 4 2KB + 8 2KB + 12 free(pi); (free) 2KB(invalid) Address Space *pi 16KB 2KB heap (free) stack *pi 2KB(invalid) Address Space 16KB 6 Youjip Won

  7. Forgetting To Allocate Memory Incorrect code char *src = hello ; //character string constant char *dst; //unallocated strcpy(dst, src); //segfault and die hello\0 heap unallocated strcpy(dst, src); (free) stack *dst *src Address Space 7 Youjip Won

  8. Forgetting To Allocate Memory(Cont.) Correct code char *src = hello ; char *dst (char *)malloc(strlen(src) + 1 ); // allocated strcpy(dst, src); //work properly //character string constant hello\0 hello\0 allocated hello\0 strcpy(dst, src); heap heap (free) stack (free) stack *dst *src *dst *src Address Space Address Space 8 Youjip Won

  9. Not Allocating Enough Memory Incorrect code, but work properly char *src = hello ; //character string constant char *dst (char *)malloc(strlen(src)); // too small strcpy(dst, src); //work properly h e l l o strlen 6 bytes \0 hello\0 5 bytes \0 is omitted heap strcpy(dst, src); (free) stack *dst *src Address Space Youjip Won

  10. Forgetting to Initialize Encounter an uninitialized read int *x = (int *)malloc(sizeof(int)); // allocated printf( *x = %d\n , *x); // uninitialized memory access value used before (free) allocated with value used before heap heap (free) (free) stack stack *x *x Address Space Address Space 10 Youjip Won

  11. Memory Leak A program runs out of memory and eventually dies. : unused, but not freed unused allocated unused unused allocated unused heap unused heap heap allocated (free) (free) stack (free) *d stack *c *b *b *a *a *a Address Space Address Space Address Space run out of memory 11 Youjip Won

  12. Dangling Pointer Freeing memory before it is finished using A program accesses to memory with an invalid pointer free() *b *b unreachable *a *a dangling pointer 2KB 2KB 3KB 3KB 3KB 3KB 4KB freed free(b) 4KB 4KB NULL NULL Heap Heap (free) (free) Stack Stack *b *a *b *a 3KB 3KB 2KB 2KB Address Space Address Space 12 Youjip Won

  13. Other Memory APIs: calloc() #include <stdlib.h> void *calloc(size_t num, size_t size) Allocate memory on the heap and zeroes it before returning. Argument size_t num : number of blocks to allocate size_t size : size of each block(in bytes) Return Success : a void type pointer to the memory block allocated by calloc Fail : a null pointer 13 Youjip Won

  14. Double Free Free memory that was freed already. int *x = (int *)malloc(sizeof(int)); // allocated free(x); // free memory free(x); // free repeatedly 2KB 2KB allocated freed Heap Heap free(x) free(x) Undefined Error (free) (free) Stack Stack 2KB 2KB(invalid) Address Space *x *x 16KB 16KB Address Space 14 Youjip Won

  15. Other Memory APIs: realloc() #include <stdlib.h> void *realloc(void *ptr, size_t size) Change the size of memory block. A pointer returned by realloc may be either the same as ptr or a new. Argument void *ptr: Pointer to memory block allocated with malloc, calloc or realloc size_t size: New size for the memory block(in bytes) Return Success: Void type pointer to the memory block Fail : Null pointer 15 Youjip Won

  16. System Calls #include <unistd.h> int brk(void *addr) void *sbrk(intptr_t increment); malloc library call use brk system call. brk is called to expand the program s break. break: The location of the end of the heap in address space sbrk is an additional call similar with brk. Programmers should never directly call either brk or sbrk. 16 Youjip Won

  17. System Calls(Cont.) #include <sys/mman.h> void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset) mmap system call can create an anonymous memory region. 17 Youjip Won

  18. Disclaimer: This lecture slide set was initially developed for Operating System course in Computer Science Dept. at Hanyang University. This lecture slide set is for OSTEP book written by Remzi and Andrea at University of Wisconsin. 18 Youjip Won

More Related Content