Memory allocation

23
Memory allocation in computer science, is the act of allocating memory to a program for its usage, typically for storing variables, code or data. Memory allocation is a widely discussed topic in the field of operating systems, as computers have limited memory, and many programs need memory to run. To find out more, you can look up on the various memory allocation algorithms and techniques that can be used.

description

Memory allocation. in computer science, is the act of allocating memory to a program for its usage, typically for storing variables, code or data. - PowerPoint PPT Presentation

Transcript of Memory allocation

Page 1: Memory allocation

Memory allocation

• in computer science, is the act of allocating memory to a program for its usage, typically for storing variables, code or data.

• Memory allocation is a widely discussed topic in the field of operating systems, as computers have limited memory, and many programs need memory to run.

• To find out more, you can look up on the various memory allocation algorithms and techniques that can be used.

Page 2: Memory allocation

Dynamic memory allocation in C

• Dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program.

• Memory is typically allocated from a large pool of all available unused memory called the heap, but may also be allocated from multiple pools.

Page 3: Memory allocation

Dynamic Memory Allocation

• In dynamic memory allocation is done during runtime.

• We determine the size of memory area to be allocated and the time, when allocation is done.

• Then we can allocate space as much as we need and just when we need it.

• When we no longer need it, we free it.

Page 4: Memory allocation

Dynamic Memory Allocation

• A dynamically allocated object remains allocated until it is deallocated explicitly, either by the programmer or by an garbage collector;

Page 5: Memory allocation

Garbage Collector• In computing, garbage collection is a

system of automatic memory management which seeks to reclaim memory used by objects which will never be referenced in the future.

• It is commonly abbreviated as GC.

• The part of a system which performs garbage collection is called a garbage collector.

Page 6: Memory allocation

Garbage Collector

• When a system has a garbage collector it is usually part of the language run-time system and integrated into the language.

• The language is said to be garbage collected.

• Garbage collection was invented by John McCarthy as part of the first Lisp system.

Page 7: Memory allocation

The basic principle of how a garbage collector works is:

• 1. Determine what data objects in a program cannot be referenced in the future

• 2. Reclaim the storage used by those objects

Page 8: Memory allocation

Languages whose standard implementations use automatic

garbage collection

• BASIC C# Caml (and OCaml) D Dylan Eiffel Haskell

• Java Javascript Lisp Lua Mercury Modula-3 (one of the major differences from Modula-2 is GC)

• ML Oberon (and Oberon-2, Active Oberon, etc) • Perl Pico Python Q Ruby Scheme Smalltalk

SNOBOL • Tcl Visual Basic.NET

Page 9: Memory allocation

Dynamic Lifetime

• It is notably different from automatic and static memory allocation. We say that such an object has dynamic lifetime.

Page 10: Memory allocation

Problems

• The problem of fulfilling an allocation request, which involves finding a block of unused memory of a certain size in the heap, is a difficult problem.

• A huge variety of solutions have been proposed.

• What we are dealing with now is how to use the keyword malloc, realloc, and free.

Page 11: Memory allocation

Fragmentation

• In a computer operating system, fragmentation is a consequence of allocating and freeing differently-sized blocks of data storage.

• It results in the accumulation of small regions of free storage that are too small to be useful for allocation, even though in sum there may be more than sufficient free space.

Page 12: Memory allocation

Malloc

–malloc is a tool for allocating memory dynamically in the C programming language. Without malloc, memory allocation must be done "at once".

Page 13: Memory allocation

Example

• If we wish to create an array of ten integers, without malloc we may say

• int tbl[10];

Page 14: Memory allocation

Pointer to an array

• Alternatively we can create a pointer to the first element of the array.

• The following code will allow us to use either tbl or x to access data within the array.

• int tbl[10]; • int *x; • x = tbl;

• /* tbl[i] and x[i] are now equivalent */•

Page 15: Memory allocation

malloc

• Now this does not seem to have gained us much, but the use of malloc allows us to create an array of any size

• int *x; x = malloc(sizeof(int)*10);

/* x can now be used as an array of ten integers */

Page 16: Memory allocation

or, if the array size isn't known until run time:

• int *x = malloc(sizeof(int)*m);

• where m is a size determined dynamically

Page 17: Memory allocation

realloc

• If you later determine that the size of the array has to be changed, you can use the realloc function to do this.

• x = realloc(x, m2)

• /* resize array to m2 elements */

Page 18: Memory allocation

free

• If you use malloc, then when you are done with the array, you have to free it:

• free(x);

• /* release the memory */

• Example: strchrex.c week07

Page 19: Memory allocation

Buffer overflow

• A buffer overflow is a type of computer bug. • When the length limitation of a space reserved

for data - a buffer - is not properly enforced, the buffer "overflows".

• Input data is written to the buffer and, if it is longer than the buffer size, the space beyond the end of the buffer is overwritten.

• This might corrupt other data, or more seriously, the program code.

Page 20: Memory allocation

Multi -- Dimensional Array

• float two [3] [4]; declared, unintialized

• float two [3] [4] = { {3.1, 7.8, 9.4, 2.2},

• {5.3, 8.3, 1.4, 2.7},

• {6.5, 7.7, 8.8, 9.9}

• };

• Declared and initialized.

Page 21: Memory allocation

Multi--dimensional array

• Or:

• float two [ ] [4] = { {3.1, 7.8, 9.4, 2.2},

• {5.3, 8.3, 1.4, 2.7},

• {6.5, 7.7, 8.8, 9.9}

• };

Page 22: Memory allocation

How about 3-D array?

• Int array3 [2] [3] [4] = { • { { 11, 12, 13, 14},• { 21, 22, 23, 24},• { 31, 32, 33, 34},• },• { { 21, 22, 23, 24},• { 25, 26, 27, 28},• { 29, 30, 31, 32}• }• };

Page 23: Memory allocation

How do we comprehend m-D array?

• 1. Two dimensional array is an array of one-dimensional arrays.

• 2. Three dimensional array is an array of two dimensional arrays.

• Example: for a 2-D array,

• int twod [3] [4]; or

• int (twod [ 3] ) [4];