Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,

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.
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
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.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
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’;
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.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Pointers Applications
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Outline Midterm results Static variables Memory model
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Dynamic memory allocation and Pointers Lecture 4.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
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.
Stack and Heap Memory Stack resident variables include:
Dynamic Allocation Review Structure and list processing
Day 03 Introduction to C.
ENEE150 Discussion 07 Section 0101 Adam Wang.
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
CS111 Computer Programming
Memory Allocation CS 217.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
C (and C++) Pointers April 4, 2019.
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Dynamic Data Structures
Presentation transcript:

Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility, there are four library routines known as “memory management functions” that can be used for allocating and freeing memory during program execution. These functions are: 1.malloc() 2.Calloc() 3.Realloc() 4.Free() These functions are defined either in the header file “alloc.h”or in stdlib.h” or in both

Memory Allocation Process Memory allocation process associated with a C program. The RAM can divided into 4 areas. Code Area Static and global variables Free memory or Heap Local variable or stack

Memory Allocation Process Memory allocation process associated with a C program. The RAM can divided into 4 areas. 1.Local variables are stored in area called stack. 1.The code area is the region in the RAM where your C program instructions, after they are translated into machine language are stored. 1.There is a separate area for storing the global and static variables. 1.The free memory area is called the heap. The size of heap keeps changing when program is executed due to creation and deletion of variables.

Memory Allocation Process 1)malloc in C : The function most commonly used for dynamic memory allocation is malloc(). The syntax of the function is malloc(x); Where x is an unsigned integer which stands for the number of bytes you want to draw from the heap. The function returns a pointer, if your request is successful. Otherwise malloc return a void pointer. If you want it to point to any datatype of data you should write (data_tpye*)malloc(x);

Memory Allocation Process 1)malloc in C : consider the following ststement ptr=(int*)malloc(sizeof(int)); In this example,a memory space equivalent to size of an int byte is reserved and the address of first byte of memory allocated is assigned to the pointer ptr of type of int. e.g. ptr= (char*) malloc(5); Allocate 5 byte of space for the pointer ptr of type char as shown ptr address of first byte We can also use malloc to allocate memory for complex data type such as structures

#include #include #include void main() { int a,*ptr; a=10; ptr=(int*)malloc(a*sizeof(int)); ptr=a; printf("%d",ptr); free(ptr); getch(); } Sample Program : malloc.c Program Explanation : 1.#include header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files. 2.#include is used because the C in- built function getch() comes under conio.h header files. 3.stdlib.h is used because malloc() comes under it. 4.int type variable a and pointer *ptr are declared. 5.Variable a is assigned a value of malloc() is used to allocate memory to pointer ptr. 7.The size given through malloc() to ptr is sizeof(int). 8.Now ptr is assigned the value of a. ptr=a; so the value 10 is assigned to ptr, for which, we dynamically allocated memory space using malloc. 9.The value in ptr is displayed using printf 10.Then allocated memory is freed using the C in-built function free().

Memory Allocation Process 1)Calloc in C : Function calloc (),like malloc() allocates memory in a dynamic manner. It takes,as arguments, two valus p=(t*)calloc(n,b) the above statement allocate n blocks of memory,each block with b bytes. If there is not enough space, a NULL pointer is returned. this function is useful for storing arrays. If each element of an array requires b bytes and the array is required to store K elements we can allocate memory by the following statement p=(t*)calloc(k,b)

pointer=(data_type*)calloc(no of memory blocks, size of each block in bytes); Syntax : #include #include #include void main() { int *ptr,a[6]={1,2,3,4,5,6}; int i; ptr=(int*)calloc(a[6]*sizeof(int),2); for(i=0;i<7;i++) { printf("\n %d",*ptr+a[i]); } free(ptr); getch(); } Sample Program : calloc.c

Explanation : 1.#include header file is included because, the C in-built statement printf we used in this program comes under stdio.h header files. 2.#include is used because the C in-built function getch() comes under conio.h header files. 3.stdlib.h is used because malloc() comes under it. 4.A Pointer *ptr of type int and array a[6] is declared. 5.Array a[6] is assigned with 6 values. 6.Another variable i of type int is declared. 7.Calloc() is used to allocate memory for ptr. 6 blocks of memory is allocated with each block having 2 bytes of space. 8.Now variable i is used in for to cycle the loop 6 times on incremental mode. 9.On each cycle the data in allocated memory in ptr is printed using *ptr+a[i]. 10.Then the memory space is freed using free(ptr).

Memory Allocation Process 1)realloc in C : we can change the memory size already allocated with the help of the funcation realloc. This process is called the reallocation of the memory. For example, if the original allocation is done on the statement ptr= malloc(size) Then the reallocation of the space may be done by the statement ptr= malloc( ptr, newsize)

Memory Allocation Process 1)Free()in C : the memory must be returned to the heap, when it is no longer required. This is done with free() function. The syntax of the statement is free (ptr) Where ptr is the pointer to a block of memory which has been allocated from the heap on request. The function is void in nature and does not return anything.