Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area

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.
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.
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.
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.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
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’;
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
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.
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.
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:
Computer Organization and Design Pointers, Arrays and Strings in C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 4.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Day 03 Introduction to C.
Introduction to Programming
2016.
Day 03 Introduction to C.
Lecture 6 C++ Programming
Dynamic Memory Allocation
Some examples.
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
14th September IIT Kanpur
Programming and Data Structures
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Weeks 7-8 Memory allocation Pointers functions
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Presentation transcript:

Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area Hrs 10 Dynamic Memory Allocation 2 Memory allocation functions (malloc, calloc, realloc, etc.) Memory de-allocation function (free) KIIT UNIVERSITY

Dynamic Memory Allocation The exact size of array is unknown until the compile time i.e. time when a compiler compiles code written in a programming language into a executable form. The size of array you have declared initially can be sometimes insufficient and sometimes more than required. What? The process of allocating memory during program execution is called dynamic memory allocation. It also allows a program to obtain more memory space, while running or to release space when no space is required. Difference between static and dynamic memory allocation Sr # Static Memory Allocation Dynamic Memory Allocation 1 User requested memory will be allocated at compile time that sometimes insufficient and sometimes more than required. Memory is allocated while executing the program. 2 Memory size can’t be modified while execution. Memory size can be modified while execution. KIIT UNIVERSITY

Dynamic Memory Allocation cont… Functions available in C for memory management Sr # Function Description 1 void *calloc(int num, int size) Allocates an array of num elements each of which size in bytes will be size. 2 void free(void *address) Releases a block of memory block specified by address. 3 void *malloc(int num) Allocates an array of num bytes and leave them initialized. 4 void *realloc(void *address, int newsize) Re-allocates memory extending it up to new size. KIIT UNIVERSITY

Dynamic Memory Allocation cont… include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char name[100]; char *description; strcpy(name, “Rajat Behera"); /* allocate memory dynamically */ description = malloc( 200 * sizeof(char) ); if( description == NULL ) printf("Error - unable to allocate required memory\n"); return; } else strcpy( description, “we are studying DMA"); printf("Name = %s\n", name ); printf("Description: %s\n", description ); free (description) KIIT UNIVERSITY

Dynamic Memory Allocation cont… #include <stdio.h> #include <stdlib.h> int main() { char *mem_allocation; /* memory is allocated dynamically */ mem_allocation = malloc( 20 * sizeof(char) ); if( mem_allocation == NULL ) printf("Couldn't able to allocate requested memory\n"); return; } else strcpy( mem_allocation,“dynamic memory allocation for realloc function"); printf("Dynamically allocated memory content : %s\n", mem_allocation ); mem_allocation=realloc(mem_allocation,100*sizeof(char)); printf("Couldn't able to allocate requested memory\n"); strcpy( mem_allocation,"space is extended upto 100 characters"); printf("Resized memory : %s\n", mem_allocation ); free(mem_allocation); return 0; C Program illustrating the usage of realloc KIIT UNIVERSITY

Dynamic Memory Allocation cont… Difference between calloc and malloc Sr # malloc calloc 1 It allocates only single block of requested memory It allocates multiple blocks of requested memory 2 doesn’t initializes the allocated memory. It contains garbage values. initializes the allocated memory to zero 3 int *ptr; ptr = malloc( 20 * sizeof(int)); For the above, 20*2 bytes of memory only allocated in one block. Total = 40 bytes Ptr = calloc( 20, 20 * sizeof(int)); For the above, 20 blocks of memory will be created and each contains 20*2 bytes of memory. Total = 800 bytes KIIT UNIVERSITY

Class Work (CW) KIIT UNIVERSITY Find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc() function. #include <stdio.h> #include <stdlib.h> int main() { int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc if(ptr==NULL) printf("Error! memory not allocated."); return; } printf("Enter elements of array: "); for(i=0;i<n;++i) scanf("%d",ptr+i); sum+=*(ptr+i); printf("Sum=%d",sum); free(ptr); return 0; KIIT UNIVERSITY

Class Work (CW) KIIT UNIVERSITY Find Largest Element Using Dynamic Memory Allocation. To perform this program, allocate memory dynamically using calloc() function. #include <stdio.h> #include <stdlib.h> int main() { int i,n; float *data; printf("Enter total number of elements(1 to 100): "); scanf("%d",&n); data=(float*)calloc(n,sizeof(float)); /* Allocates the memory for 'n' elements */ if(data==NULL) printf("Error!!! memory not allocated."); return; } printf("\n"); for(i=0;i<n;++i) /* Stores number entered by user. */ printf("Enter Number %d: ",i+1); scanf("%f",data+i); for(i=1;i<n;++i) /* Loop to store largest number at address data */ if(*data<*(data+i)) /* Change < to > if you want to find smallest number */ *data=*(data+i); printf("Largest element = %.2f",*data); free (data); return 0; KIIT UNIVERSITY

Thank You KIIT UNIVERSITY