Memory allocation in computer science is the act

  • Slides: 23
Download presentation
Memory allocation • in computer science, is the act of allocating memory to a

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.

Dynamic memory allocation in C • Dynamic memory allocation is the allocation of memory

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.

Dynamic Memory Allocation • In dynamic memory allocation is done during runtime. • We

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.

Dynamic Memory Allocation • A dynamically allocated object remains allocated until it is deallocated

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

Dynamic Lifetime • It is notably different from automatic and static memory allocation. We

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

Problems • The problem of fulfilling an allocation request, which involves finding a block

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.

Fragmentation • In a computer operating system, fragmentation is a consequence of allocating and

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.

Malloc – malloc is a tool for allocating memory dynamically in the C programming

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

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

Example • If we wish to create an array of ten integers, without malloc we may say • int tbl[10];

What is garbage collection? • Garbage collection is a part of a language's runtime

What is garbage collection? • Garbage collection is a part of a language's runtime system. • Or an add-on library, perhaps assisted by the compiler, the hardware, the OS, or any combination of the three. • It automatically determines what memory a program is no longer using, and recycles it for other use. • It is also known as ``automatic storage (or memory) reclamation''.

garbage collector • In computing, garbage collection is a system of automatic memory management

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.

Pointer to an array • Alternatively we can create a pointer to the first

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 */ •

malloc • Now this does not seem to have gained us much, but the

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 */ •

or, if the array size isn't known until run time: • int *x =

or, if the array size isn't known until run time: • int *x = malloc(sizeof(int)*m); • /* where m is a size determined dynamically */ • for (i=0; i

realloc • If you later determine that the size of the array has to

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, m 2) • /* resize array to m 2 elements */

free • If you use malloc, then when you are done with the array,

free • If you use malloc, then when you are done with the array, you have to free it: • free(x); • /* release the memory */

Buffer overflow • A buffer overflow is a type of computer bug. • When

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.

Example • Frquency. c -- frequency • week 011

Example • Frquency. c -- frequency • week 011

Multi -- Dimensional Array • float two [3] [4]; declared, unintialized • float two

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.

Multi--dimensional array • Or: • float two [ ] [4] = { {3. 1,

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} • };

How about 3 -D array? • Int array 3 [2] [3] [4] = {

How about 3 -D array? • Int array 3 [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} • };

How do we comprehend m-D array? • 1. Two dimensional array is an array

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]; •