SlideShare a Scribd company logo
1 of 14
What is Memory?
Computer memory is any physical device capable of storing
information temporarily or permanently.
Memory is the processes by which information is encoded,
stored and retrieved.
Type of Memory Allocation
There are two type of memory allocation.
1) Static memory allocation.
2) Dynamic memory allocation.
Different Between Static and dynamic memory allocation.
• Static memory allocation is allocated the memory at
the compile time. Dynamic at the runtime of
execution of program.
• In static memory can’t be increase while execution of
the program. But in the dynamic memory can
increase while executing the program.
• Static used in array and dynamic used in linked list.
What is Dynamic memory
Allocation?
Memory space required can be specified at the time of
execution.
The concept of dynamic memory allocation in c
language enables the C programmer to allocate memory
at runtime. Dynamic memory allocation in c language is
possible by 4 functions of stdlib.h header file.
malloc()
calloc()
realloc()
free()
What is Allocation?
An allocation is something that you set aside for use. For
instance if you want to set aside a certain amount of hard
drive space for an application.
Memory allocation is the process of setting aside section
of memory in a program to be used to store variables, and
instances of structure and classes.
.
malloc()
The declaration of malloc ( ) function is
Syntax:-
Void*malloc(size_t size)
Example:- int *nums = (int*)malloc(5*sizeof(int));
This is similar to int *nums=new int[5];
Deallocation of using free[] function-
free(nums);
This is special function that are used to assign value to
variable.
New return exact datatype, while malloc() return void
pointer.
To use malloc() you must #include<stdlib.h>
malloc() …continue….
 On error malloc()return NULL.
 If size =0 malloc() also return NULL.
 The malloc function allocates a block of size bytes from
the memory heap.
 It allows a program to allocates memory as it’s needed
and in the exact , amount needed.
 On success malloc() return a pointer to the newely
allocated block of memory.
malloc()….. Continue..
 char *charpt; /* declare a pointer to char */
 charpt
 charpt = malloc(10);
10 bytes or chars
charpt
 charpt contains the address of the beginning of that
block.
calloc()
 The calloc() function allocates multiple block of
requested memory.
 It returns NULL if memory is not sufficient.
 The declaration of calloc ( ) function is –
void *calloc(size_t n items , size_t size)
Example:- ptr = (float*) calloc(25, sizeof(float));
 calloc() provided access in c memory heap. which is
available for dynaminc allocation of variable size block
of memory.
calloc()… continue..
 The name calloc stands for "contiguous allocation".
 The only difference between malloc() and calloc() is
that, malloc() allocates single block of memory whereas
calloc() allocates multiple blocks of memory each of
same size and sets all bytes to zero.
 Allocates space for an array of elements, initializes them
to zero and then returns a pointer.
realloc()
 Reallocates the memory occupied by malloc() or calloc()
functions.
Syntax:-
void *realloc(void *ptr, size_t newsize);
Let’s take an example:-
int *ptr;
// allocate memory for 5 integers
ptr = (int*)malloc(5*sizeof(int));
// allocate memory for 6 more integers i.e a total of 11.
ptr = (int*)realloc(ptr, 11*sizeof(int));
realloc() ….continue..
#include <stdio.h>
#include <stdlib.h>
int main () {
char *str; clrscr();
/* Initial memory allocation */
str = (char *) malloc(5);
strcpy(str, "kiran");
printf("String = %s, Address = %un", str, str);
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, "patel");
printf("String = %s, Address = %un", str, str);
free(str);
getch();}
Output:-
free()
 The C library function void free(void *ptr) deallocates
the memory previously allocated by a call to calloc,
malloc, or realloc.
Syntax:-
void free(void *ptr)
Example:-
Int main(){
char *str;
-----
-----
free(str);
}
Dynamic memory Allocation in c language

More Related Content

What's hot

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationViji B
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming Kamal Acharya
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationNaveen Gupta
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languagetanmaymodi4
 
Union in c language
Union  in c languageUnion  in c language
Union in c languagetanmaymodi4
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 

What's hot (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
C functions
C functionsC functions
C functions
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
File in C language
File in C languageFile in C language
File in C language
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 

Similar to Dynamic memory Allocation in c language

Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTAkhilMishra50
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Mangalayatan university
 
Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In CSimplilearn
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
Dma
DmaDma
DmaAcad
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c languageTanmay Modi
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationShu-Yu Fu
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptxBilalImran17
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGaurav Mandal
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationUTTAM VERMA
 
ECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docxECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docxtidwellveronique
 
ECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docxECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docxtidwellveronique
 

Similar to Dynamic memory Allocation in c language (20)

Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
 
Dma
DmaDma
Dma
 
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
 
Dynamic Memory Allocation In C
Dynamic Memory Allocation In CDynamic Memory Allocation In C
Dynamic Memory Allocation In C
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
Dma
DmaDma
Dma
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Stack & heap
Stack & heap Stack & heap
Stack & heap
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
 
final GROUP 4.pptx
final GROUP 4.pptxfinal GROUP 4.pptx
final GROUP 4.pptx
 
TLPI - 7 Memory Allocation
TLPI - 7 Memory AllocationTLPI - 7 Memory Allocation
TLPI - 7 Memory Allocation
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
Embedded C - Lecture 3
Embedded C - Lecture 3Embedded C - Lecture 3
Embedded C - Lecture 3
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
ECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docxECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docx
 
ECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docxECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docx
 

More from kiran Patel

2017 Union budget of India
2017 Union budget of India 2017 Union budget of India
2017 Union budget of India kiran Patel
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphismkiran Patel
 
Database Management System( Normalization)
Database Management System( Normalization)Database Management System( Normalization)
Database Management System( Normalization)kiran Patel
 
B tree (computer Science)
B tree (computer Science)B tree (computer Science)
B tree (computer Science)kiran Patel
 
Effort estimation( software Engineering)
Effort estimation( software Engineering)Effort estimation( software Engineering)
Effort estimation( software Engineering)kiran Patel
 
Thread (Operating System)
Thread  (Operating System)Thread  (Operating System)
Thread (Operating System)kiran Patel
 
Library management (use case diagram Software engineering)
Library management (use case  diagram Software engineering)Library management (use case  diagram Software engineering)
Library management (use case diagram Software engineering)kiran Patel
 
Brain Computer Interface
Brain Computer InterfaceBrain Computer Interface
Brain Computer Interfacekiran Patel
 
Artificial Inteligence
Artificial InteligenceArtificial Inteligence
Artificial Inteligencekiran Patel
 
Smart buckets ppt
Smart buckets pptSmart buckets ppt
Smart buckets pptkiran Patel
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocationkiran Patel
 

More from kiran Patel (11)

2017 Union budget of India
2017 Union budget of India 2017 Union budget of India
2017 Union budget of India
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
Database Management System( Normalization)
Database Management System( Normalization)Database Management System( Normalization)
Database Management System( Normalization)
 
B tree (computer Science)
B tree (computer Science)B tree (computer Science)
B tree (computer Science)
 
Effort estimation( software Engineering)
Effort estimation( software Engineering)Effort estimation( software Engineering)
Effort estimation( software Engineering)
 
Thread (Operating System)
Thread  (Operating System)Thread  (Operating System)
Thread (Operating System)
 
Library management (use case diagram Software engineering)
Library management (use case  diagram Software engineering)Library management (use case  diagram Software engineering)
Library management (use case diagram Software engineering)
 
Brain Computer Interface
Brain Computer InterfaceBrain Computer Interface
Brain Computer Interface
 
Artificial Inteligence
Artificial InteligenceArtificial Inteligence
Artificial Inteligence
 
Smart buckets ppt
Smart buckets pptSmart buckets ppt
Smart buckets ppt
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Dynamic memory Allocation in c language

  • 1.
  • 2. What is Memory? Computer memory is any physical device capable of storing information temporarily or permanently. Memory is the processes by which information is encoded, stored and retrieved.
  • 3. Type of Memory Allocation There are two type of memory allocation. 1) Static memory allocation. 2) Dynamic memory allocation. Different Between Static and dynamic memory allocation. • Static memory allocation is allocated the memory at the compile time. Dynamic at the runtime of execution of program. • In static memory can’t be increase while execution of the program. But in the dynamic memory can increase while executing the program. • Static used in array and dynamic used in linked list.
  • 4. What is Dynamic memory Allocation? Memory space required can be specified at the time of execution. The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file. malloc() calloc() realloc() free()
  • 5. What is Allocation? An allocation is something that you set aside for use. For instance if you want to set aside a certain amount of hard drive space for an application. Memory allocation is the process of setting aside section of memory in a program to be used to store variables, and instances of structure and classes. .
  • 6. malloc() The declaration of malloc ( ) function is Syntax:- Void*malloc(size_t size) Example:- int *nums = (int*)malloc(5*sizeof(int)); This is similar to int *nums=new int[5]; Deallocation of using free[] function- free(nums); This is special function that are used to assign value to variable. New return exact datatype, while malloc() return void pointer. To use malloc() you must #include<stdlib.h>
  • 7. malloc() …continue….  On error malloc()return NULL.  If size =0 malloc() also return NULL.  The malloc function allocates a block of size bytes from the memory heap.  It allows a program to allocates memory as it’s needed and in the exact , amount needed.  On success malloc() return a pointer to the newely allocated block of memory.
  • 8. malloc()….. Continue..  char *charpt; /* declare a pointer to char */  charpt  charpt = malloc(10); 10 bytes or chars charpt  charpt contains the address of the beginning of that block.
  • 9. calloc()  The calloc() function allocates multiple block of requested memory.  It returns NULL if memory is not sufficient.  The declaration of calloc ( ) function is – void *calloc(size_t n items , size_t size) Example:- ptr = (float*) calloc(25, sizeof(float));  calloc() provided access in c memory heap. which is available for dynaminc allocation of variable size block of memory.
  • 10. calloc()… continue..  The name calloc stands for "contiguous allocation".  The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.  Allocates space for an array of elements, initializes them to zero and then returns a pointer.
  • 11. realloc()  Reallocates the memory occupied by malloc() or calloc() functions. Syntax:- void *realloc(void *ptr, size_t newsize); Let’s take an example:- int *ptr; // allocate memory for 5 integers ptr = (int*)malloc(5*sizeof(int)); // allocate memory for 6 more integers i.e a total of 11. ptr = (int*)realloc(ptr, 11*sizeof(int));
  • 12. realloc() ….continue.. #include <stdio.h> #include <stdlib.h> int main () { char *str; clrscr(); /* Initial memory allocation */ str = (char *) malloc(5); strcpy(str, "kiran"); printf("String = %s, Address = %un", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, "patel"); printf("String = %s, Address = %un", str, str); free(str); getch();} Output:-
  • 13. free()  The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc. Syntax:- void free(void *ptr) Example:- Int main(){ char *str; ----- ----- free(str); }