+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic memory allocation
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Computer Science: A Structured Programming Approach Using C Memory Allocation Functions C gives us two choices when we want to reserve memory locations.
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.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Pointers Applications
Outline Midterm results Static variables Memory model
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Day 03 Introduction to C.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
14th September IIT Kanpur
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
Dynamic Memory Allocation
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Presentation transcript:

+ Dynamic memory allocation

+ Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers. When list grows we need to allocate more memory space to the list to accommodate additional data items. Such situation can be handled by using dynamic memory management.

+ Dynamics memory allocation C language requires the number of elements in an array to be specified at compile time. But if we wrong with initial guess of number of elements? It may cause failure of the program (not enough elements) It can was memory space (to many unused elements) The solution is to allocate memory at run time (dynamic memory allocation). C does not have such functionality, but there four library routines that allows to allocate memory dynamically. FunctionTask mallocAllocate request size of bytes and return a pointer to the first byte callocSame as malloc but initialize memory to zero freeFrees previously allocated space reallocModifies the sizes of previously allocated space

+ Memory allocation process Memory allocation process associated with a C program The program instruction, global and static variables are stored in permanent storage area. Local variables are stored in stack. The free memory is called the heap. The size is changing, it is possible to encounter memory “overflow” during dynamic allocation process. Local variables Free memory Global variables C program instruction stack heap permanent storage area

+ Allocating a block of memory: malloc The malloc function reserves a block of memory of specified size and return a pointer of type void. ptr = (cast-type *) malloc(byte-size); x = (int *) malloc(100 * sizeof(int)); cptr = (char *) malloc(10); On successful execution, a memory space will be allocated. cptr Address of first byte 10 bytes of space The malloc allocates a block of contiguous bytes.

+ Example The program uses a table of integers whose size will be specified interactively at run time.

+ Allocating multiple blocks of memory: calloc calloc allocates multiple blocks of storage, each of the same size, and then sets all bytes to zero. The general form of calloc is ptr = (cast-type *) calloc (n, elem-size); If there is not enough space, a NULL pointer is returned.

+ Releasing the used space: free Compile-time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamics run-time allocation, it is our responsibility to release the space when it is not required. The release of storage space becomes important when the storage is limited. When we no longer need the data we stored in a block memory, we may release that block of memory for future use free occupiedfree free(ptr); before after

+ Altering the size of a block: realloc It likely that we discover later: The previously allocated memory is not sufficient (add more space) The memory allocated is much larger that necessary (reduce space) We can changed allocated memory with the function realloc. ptr = malloc(size); ptr = realloc(ptr, newsize); The newsize maybe large or smaller than the size.

+ Example The program stores a character string in a block of memory space created by malloc and then modify the same to store a larger string.

+ Dynamically allocating Multidimensional arrays it's straightforward to call malloc to allocate a block of memory which can simulate an array, but with a size which we get to pick at run-time. Can we do the same sort of thing to simulate multidimensional arrays? We want to simulate an array of pointers, but we don't know how many rows there will be, either, so we'll have to simulate that array (of pointers) with another pointer, and this will be a pointer to a pointer.

+ Example

+ Dynamically allocating Multidimensional arrays If a program uses simulated, dynamically allocated multidimensional arrays, it becomes possible to write ``heterogeneous'' functions which don't have to know (at compile time) how big the ``arrays'' are. One function can operate on ``arrays'' of various sizes and shapes. func(int **array, int nrows, int ncolumns){} Example To free one of these dynamically allocated multidimensional ``arrays,'' we must remember to free each of the chunks of memory that we've allocated