memory manament in c
TRANSCRIPT
![Page 1: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/1.jpg)
Reporter: NgocVDEmail: [email protected]
![Page 2: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/2.jpg)
I. Stack:◦ Define.
◦ Stack overflow.
II. Heap:◦ Define.
◦ malloc,calloc, free, realloc
◦ In-deep of malloc
III. Region-base memory manament:◦ Obstack.
IV. GC
![Page 3: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/3.jpg)
Code segment or text segment: Code segment
contains the code executable or code binary
Data segment: Data segment is sub divided
into two parts:
◦ Initialized data segment
◦ Uninitialized data segment
Heap: allocate memory at runtime using
calloc and malloc.
Stack: is used to store your local variables
and is used for passing arguments to the
functions and return address of the
instruction which is to be executed after
the function call is over
![Page 4: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/4.jpg)
![Page 5: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/5.jpg)
![Page 6: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/6.jpg)
alloca() function allocates space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the function from which alloca() is called returns.
![Page 7: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/7.jpg)
![Page 8: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/8.jpg)
![Page 9: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/9.jpg)
![Page 10: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/10.jpg)
malloc, calloc, free, realloc
![Page 11: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/11.jpg)
![Page 12: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/12.jpg)
Base on Doug Lea Allocator
Use best-fit strategy re-use the free chunk with the smallest waste.
Coalesces chunks upon free reduce fragmentation
Use binning to find free chunks fast
memalign()
![Page 13: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/13.jpg)
Bin of memory
![Page 14: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/14.jpg)
![Page 15: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/15.jpg)
each allocated object is assigned to a region
efficiently deallocated all at once.
Regions are independent
![Page 16: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/16.jpg)
Advantages:◦ Fast allocation/de-allocation possible
◦ Very good for phase-local data (data that is only used in a certain phase in the program)
Disadvantages:◦ Potential large waste of memory
◦ Memory is organized as a stack:
Allocation/freeing sets the stack mark
Cannot free single chunks inside the stack
◦ Can be used to “grow” an object:
◦ Size of the object is not yet known at allocation site
![Page 17: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/17.jpg)
![Page 18: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/18.jpg)
Growing objects
Extra fast growing objects
![Page 19: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/19.jpg)
![Page 20: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/20.jpg)
Stack with multi-thread?
Buffer overflow protection
Boehm garbage collector
![Page 21: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/21.jpg)
http://www.inf.udec.cl/~leo/teoX.pdf
http://www.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/script/lecture09.pdf
http://en.wikipedia.org/wiki/Region-based_memory_management
http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_3.html
http://en.wikipedia.org/wiki/Boehm_garbage_collector
![Page 22: Memory manament in C](https://reader030.vdocument.in/reader030/viewer/2022020123/55a92f0b1a28ab34578b4793/html5/thumbnails/22.jpg)