SlideShare a Scribd company logo
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 in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
Tanmay Modi
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
vaani pathak
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
Mahesh Tibrewal
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Loops in C
Loops in CLoops in C
Loops in C
Kamal Acharya
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
Nitesh Bichwani
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Burhanuddin Kapadia
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Array in c
Array in cArray in c
Array in c
Ravi Gelani
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Muhammed Thanveer M
 

What's hot (20)

Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
File in C language
File in C languageFile in C language
File in C language
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Strings in C
Strings in CStrings in C
Strings in C
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Loops in C
Loops in CLoops in C
Loops in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Array in c
Array in cArray in c
Array in c
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 

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 PPT
AkhilMishra50
 
Dma
DmaDma
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 C
Simplilearn
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
Dma
DmaDma
Dma
Acad
 
C- language Lecture 6
C- language Lecture 6C- language Lecture 6
C- language Lecture 6
Hatem Abd El-Salam
 
Dynamic memory allocation in c language
Dynamic memory allocation in c languageDynamic memory allocation in c language
Dynamic memory allocation in c language
tanmaymodi4
 
final GROUP 4.pptx
final GROUP 4.pptxfinal GROUP 4.pptx
final GROUP 4.pptx
ngonidzashemutsipa
 
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.pptx
BilalImran17
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Gaurav Mandal
 
Introduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptxIntroduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptx
poongothai11
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
babuk110
 
Embedded C - Lecture 3
Embedded C - Lecture 3Embedded C - Lecture 3
Embedded C - Lecture 3
Mohamed Abdallah
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
UTTAM 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.docx
tidwellveronique
 

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
 
Introduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptxIntroduction to Data Structures, Data Structures using C.pptx
Introduction to Data Structures, Data Structures using C.pptx
 
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
 

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 Polymorphism
kiran 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 Interface
kiran Patel
 
Artificial Inteligence
Artificial InteligenceArtificial Inteligence
Artificial Inteligence
kiran Patel
 
Smart buckets ppt
Smart buckets pptSmart buckets ppt
Smart buckets ppt
kiran Patel
 
Linked list using Dynamic Memory Allocation
Linked list using Dynamic Memory AllocationLinked list using Dynamic Memory Allocation
Linked list using Dynamic Memory Allocation
kiran 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

Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 

Recently uploaded (20)

Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 

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); }