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.

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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
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.
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.
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.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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:
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.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Pointers Applications
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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.
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
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
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.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
From C to C++ Jyh-Shing Roger Jang (張智星)
CSE 220 – C Programming malloc, calloc, realloc.
Introduction to Programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Programmazione I a.a. 2017/2018.
Checking Memory Management
Dynamic Memory Allocation
14th September IIT Kanpur
Memory Allocation Functions
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Dynamic Memory – A Review
Presentation transcript:

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 or decrease as your program runs. Up until this point, the memory allocation for your program has been handled automatically when compiling. However, sometimes the computer doesn't know how much memory to set aside (for example, when you have an unsized array). The dynamic memory allocation functions give you the power to dynamically allocate memory for your variables at RUN- TIME (while the program is running). Before for the programs, memory was allocated when the program was compiled (i.e.COMPILE-TIME).

MEMORY ALLOCATION FUNCTIONS malloc() malloc() calloc() calloc() realloc() realloc() free() free()

This function is used to allocate memory space in bytes to the variables of different types. This function is used to allocate memory space in bytes to the variables of different types. malloc requires one argument - the number of bytes you want to allocate dynamically. malloc requires one argument - the number of bytes you want to allocate dynamically. If the memory allocation was successful, malloc will return base address of allocated memory to this to a pointer variable. If the memory allocation was successful, malloc will return base address of allocated memory to this to a pointer variable. The prototypes are declared in alloc.h and stdlib.h The prototypes are declared in alloc.h and stdlib.h

int *ptr; ptr=(datatype*) malloc(size); ptr=(int*) malloc(20);

#include<alloc.h> Void main() { int k,*p,j=0; printf(“how many numbers?”); scanf(“%d”,&k);p=(int*)malloc(k*sizeof(int));while(j!=k){printf(“number=%d”,j+1);scanf(“%d”,p+j);j++;}

j=0; printf(“the numbers are:”); while(j!=k)printf(“%d”,*(p+j));j++;}

calloc() This function is used for allocating multiple blocks of memory. This function is used for allocating multiple blocks of memory. It is declared with two arguments. It is declared with two arguments. The format is as follows: The format is as follows:ptr=(int*)calloc(4,2); It allocates 4 blocks of memory and each block contains 2 bytes. It allocates 4 blocks of memory and each block contains 2 bytes.

#include<alloc.h> Void main() { int k,*p,j=0; printf(“how many numbers?”); scanf(“%d”,&k);p=(int*)calloc(k*sizeof(int),2);while(j!=k){printf(“number=%d”,j+1);scanf(“%d”,p+j);j++;}

j=0; printf(“the numbers are:”); while(j!=k)printf(“%d”,*(p+j));j++;}

realloc() This function reallocates main memory. This function reallocates main memory. It is used to shrink or enlarge the previously allocated memory with malloc() and calloc(). It is used to shrink or enlarge the previously allocated memory with malloc() and calloc().Ptr=(int*)malloc(20);Ptr=(int*)realloc(ptr,10);

free() This function is used to release the memory allocated by the memory allocating functions. This function is used to release the memory allocated by the memory allocating functions. Using this function the wastage of memory is prevented. Using this function the wastage of memory is prevented.free(ptr);

Memory Leak A condition caused by a program that does not free up the extra memory it allocates. In programming languages, such as C/C++, the programmer can dynamically allocate additional memory to hold data and variables that are required for the moment, but not used throughout the program. When those memory areas are no longer needed, the programmer must remember to deallocate them. A condition caused by a program that does not free up the extra memory it allocates. In programming languages, such as C/C++, the programmer can dynamically allocate additional memory to hold data and variables that are required for the moment, but not used throughout the program. When those memory areas are no longer needed, the programmer must remember to deallocate them.

Memory Leak(Contd..) When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory leaks occur, they can usurp all of memory and bring everything to a halt or slow the processing considerably. When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory leaks occur, they can usurp all of memory and bring everything to a halt or slow the processing considerably.