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.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Names and Bindings.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Data Types-II: Composite Data Types.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Chapter 5 Names, Bindings, and Scopes
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Pointers Applications
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Outline Midterm results Static variables Memory model
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Sather vs Java §Brian Oh §Ann Win. l Introduction (of Sather) l History (of Sather) l Comparison between Java and Sather l Sample - “Hello World” l Future.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Semantics of Arrays and Pointers By: M. Reza Heydarian Introduction Pointers Arrays Semantics of Arrays Semantics of Pointers.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Storage 18-May-18.
CS 326 Programming Languages, Concepts and Implementation
Storage Management.
The Hardware/Software Interface CSE351 Winter 2013
Heap storage & Garbage collection
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
Presentation transcript:

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 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 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 explicitly, either by the programmer or by an garbage collector; garbage collector

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.memory management It is commonly abbreviated as GC. The part of a system which performs garbage collection is called a garbage collector.

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

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

Languages whose standard implementations use automatic garbage collection BASIC C# Caml (and OCaml) D Dylan Eiffel HaskellBASICC#CamlOCamlDDylan EiffelHaskell Java Javascript Lisp Lua Mercury Modula-3 (one of the major differences from Modula-2 is GC)JavaJavascriptLispLuaMercury Modula-3Modula-2 ML Oberon (and Oberon-2, Active Oberon, etc)MLOberonOberon-2 Perl Pico Python Q Ruby Scheme Smalltalk SNOBOLPerlPicoPythonQRubySchemeSmalltalk SNOBOL Tcl Visual Basic.NETTclVisual Basic.NET

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 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 freeing differently-sized blocks of data storage.computeroperating system 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 language. Without malloc, memory allocation must be done "at once".C programming languagememory allocation

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

Pointer to an array Alternatively we can create a pointer to the first element of the array.pointer 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 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 = malloc(sizeof(int)*m); where m is a size determined dynamically

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

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

Buffer overflow A buffer overflow is a type of computer bug.computer bug When the length limitation of a space reserved for data - a buffer - is not properly enforced, the buffer "overflows".buffer 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.

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

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