22
Reporter: NgocVD Email: [email protected]

Memory manament in C

Embed Size (px)

Citation preview

Page 1: Memory manament in C

Reporter: NgocVDEmail: [email protected]

Page 2: Memory manament in C

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

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
Page 5: Memory manament in C
Page 6: Memory manament in C

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
Page 8: Memory manament in C
Page 9: Memory manament in C
Page 10: Memory manament in C

malloc, calloc, free, realloc

Page 11: Memory manament in C
Page 12: Memory manament in C

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

Bin of memory

Page 14: Memory manament in C
Page 15: Memory manament in C

each allocated object is assigned to a region

efficiently deallocated all at once.

Regions are independent

Page 16: Memory manament in C

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
Page 18: Memory manament in C

Growing objects

Extra fast growing objects

Page 19: Memory manament in C
Page 20: Memory manament in C

Stack with multi-thread?

Buffer overflow protection

Boehm garbage collector

Page 21: Memory manament in C

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