Memory manament in C

Post on 17-Jul-2015

91 views 8 download

Tags:

transcript

Reporter: NgocVDEmail: vudangngoc@nhn.com

I. Stack:◦ Define.

◦ Stack overflow.

II. Heap:◦ Define.

◦ malloc,calloc, free, realloc

◦ In-deep of malloc

III. Region-base memory manament:◦ Obstack.

IV. GC

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

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.

malloc, calloc, free, realloc

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

Bin of memory

each allocated object is assigned to a region

efficiently deallocated all at once.

Regions are independent

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

Growing objects

Extra fast growing objects

Stack with multi-thread?

Buffer overflow protection

Boehm garbage collector

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