SlideShare a Scribd company logo
1 of 9
malloc() and calloc() in C
Use of malloc():
 It allocates a block of size bytes from memory heap.
 It allows a program to allocate memory as its needed, and in the exact amount needed.
Use of calloc():
 It provides access to the C memory heap, which is available for dynamic
allocation variable-sized block of memory.
Illustrative programs:
/*Program 1: illustration of malloc()*/
#include<stdio.h>

malloc() comes under it

#include<conio.h>
#include<stdlib.h>
void main()
{
int a,*ptr;
a=10;
ptr=(int*)malloc(a*sizeof(int));
ptr=a;

Allocating memory to ptr
Illustrative programs(contd.):
/*program 1 contd.*/
printf(“%d”,ptr);
free(ptr);

Allocated memory is freed

getch();

}
In this program the sizeof(int) is the size given thorugh malloc() to ptr.
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.
Illustrative programs(contd.):
Output of program 1 is:
Ilustrative programs(contd.):
/*Program 2: Illustration of calloc()*/
#include<stdio.h>

calloc() comes under it

#include<conio.h>
#include<stdlib.h>

void main()
{
int *ptr,a[6]={1,2,3,4,5,6};
int i;

ptr=(int*)calloc(a[6]*sizeof(int),2);

Allocating memory for ptr
Illustrative programs(contd.):
/*program 2 contd.*/
for(i=0;i<7;i++)
{
printf(“n%d”,*ptr+a[i]);

}
free(ptr);
getch();
}

Allocated memory is freed
Illustrative programs(contd.):
In program 2 6 blocks of memory is allocated using calloc() with each block
having 2 bytes of space. Now variable i is used in for to cycle the loop 6
times on incremental mode. On each cycle the data in allocated memory
in ptr is printed using *ptr+a[i].

Output of program 2:

More Related Content

What's hot

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 

What's hot (20)

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
C dynamic ppt
C dynamic pptC dynamic ppt
C dynamic ppt
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
C pointer
C pointerC pointer
C pointer
 
class and objects
class and objectsclass and objects
class and objects
 
Call by value
Call by valueCall by value
Call by value
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
File in C language
File in C languageFile in C language
File in C language
 

Similar to Malloc() and calloc() in c

C programming session 01
C programming session 01C programming session 01
C programming session 01
Vivek Singh
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
Kumaran K
 

Similar to Malloc() and calloc() in c (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
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
Memory management CP
Memory management  CPMemory management  CP
Memory management CP
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 
Dma
DmaDma
Dma
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
C introduction by piyushkumar
C introduction by piyushkumarC introduction by piyushkumar
C introduction by piyushkumar
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
c.ppt
c.pptc.ppt
c.ppt
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
 
dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
 
DMA.pptx
DMA.pptxDMA.pptx
DMA.pptx
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
Dma
DmaDma
Dma
 

Recently uploaded

Recently uploaded (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 

Malloc() and calloc() in c

  • 2. Use of malloc():  It allocates a block of size bytes from memory heap.  It allows a program to allocate memory as its needed, and in the exact amount needed.
  • 3. Use of calloc():  It provides access to the C memory heap, which is available for dynamic allocation variable-sized block of memory.
  • 4. Illustrative programs: /*Program 1: illustration of malloc()*/ #include<stdio.h> malloc() comes under it #include<conio.h> #include<stdlib.h> void main() { int a,*ptr; a=10; ptr=(int*)malloc(a*sizeof(int)); ptr=a; Allocating memory to ptr
  • 5. Illustrative programs(contd.): /*program 1 contd.*/ printf(“%d”,ptr); free(ptr); Allocated memory is freed getch(); } In this program the sizeof(int) is the size given thorugh malloc() to ptr. 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.
  • 7. Ilustrative programs(contd.): /*Program 2: Illustration of calloc()*/ #include<stdio.h> calloc() comes under it #include<conio.h> #include<stdlib.h> void main() { int *ptr,a[6]={1,2,3,4,5,6}; int i; ptr=(int*)calloc(a[6]*sizeof(int),2); Allocating memory for ptr
  • 8. Illustrative programs(contd.): /*program 2 contd.*/ for(i=0;i<7;i++) { printf(“n%d”,*ptr+a[i]); } free(ptr); getch(); } Allocated memory is freed
  • 9. Illustrative programs(contd.): In program 2 6 blocks of memory is allocated using calloc() with each block having 2 bytes of space. Now variable i is used in for to cycle the loop 6 times on incremental mode. On each cycle the data in allocated memory in ptr is printed using *ptr+a[i]. Output of program 2: