html5-img
1 / 23

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.

forest
Télécharger la présentation

Memory allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. 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.

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

  5. 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.

  6. 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.

  7. 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

  8. Languages whose standard implementations use automatic garbage collection • BASICC#Caml (and OCaml) DDylanEiffelHaskell • JavaJavascriptLispLuaMercuryModula-3 (one of the major differences from Modula-2 is GC) • MLOberon (and Oberon-2, Active Oberon, etc) • PerlPicoPythonQRubySchemeSmalltalkSNOBOL • TclVisual Basic.NET

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

  10. 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.

  11. Fragmentation • In a computeroperating 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.

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

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

  14. 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 */

  15. 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 */

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

  17. 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 */

  18. 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

  19. 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.

  20. 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.

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

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

  23. 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];

More Related