SlideShare a Scribd company logo
Memory allocation in c
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Dynamic memory allocation in C
MUHAMMEDTHANVEER.M
kutubmadar@gmail.com
www.facebook.com/ thanveer
twitter.com/username
in.linkedin.com/in/profilename
9526960445
The process of allocating memory during
program execution is called dynamic memory
allocation.
4 dynamic memory allocation functions
S.No Function Syntax
1 malloc () malloc (number *sizeof(int));
2 calloc () calloc (number, sizeof(int));
3 realloc () realloc (pointer_name, number * sizeof(int));
4 free () free (pointer_name);
1. malloc() function in C:
malloc () function is used to allocate space in memory during the execution
f the program.
malloc () does not initialize the memory allocated during execution. It
carries garbage value.
malloc () function returns null pointer if it couldn’t able to allocate
requested amount of memory.
EXAMPLE
#include <stdio.h>
#include <string.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 memoryn");
}
else
{
strcpy( mem_allocation,“shernoobi@gmail.com"); output:shernoobi@gmail.com
}
printf("Dynamically allocated memory content : " 
"%sn", mem_allocation );
free(mem_allocation);
}
2. calloc() function in C:
calloc () function is also like malloc () function. But calloc ()
initializes the allocated memory to zero. But, malloc()
doesn’t.
EXAMPLE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *mem_allocation;
/* memory is allocated dynamically */
mem_allocation = calloc( 20, sizeof(char) ); Output:shernoobi@gmail.com
if( mem_allocation== NULL )
{
printf("Couldn't able to allocate requested memoryn");
}
else
{
strcpy( mem_allocation,“shernoobi@gmail.com");
}
printf("Dynamically allocated memory content : " 
"%sn", mem_allocation );
free(mem_allocation);
}
3. realloc() function in C:
realloc () function modifies the allocated memory size by malloc () and calloc () functions to new
size.
If enough space doesn’t exist in memory of current block to extend, new block is allocated for
the full size of reallocation, then copies the existing data to new block and then frees the old
block.
4. free() function in C:
free () function frees the allocated memory by malloc (), calloc (), realloc () functions
and returns the memory to the system.
EXAMPLE
//insert extra numbers
#include<stdio.h>//header
#include<stdlib.h>
int main()//main function
{
int int_n,int_i,int_j,int_m,*ptr_b;//local variable declaration
char str_a;
printf("enter number of element");//by user
scanf("%d",&int_n);//read and assign
ptr_b=(int*)calloc(int_n,sizeof(int));//malloc memory allocation
if(ptr_b==NULL)
{
printf("ERROR!memory not allocated");
exit(0);
}
printf("Now,enter the element");//by user
for(int_i=0;int_i<int_n;++int_i)//check condition
{
scanf("%d",ptr_b+int_i);//block of statement
}
printf("do you want to enter more numbers?.y/n");
scanf("%s",&str_a);
if(str_a=='y'||str_a=='Y')//check condition using if
{
printf("enter the number of extra element:");
scanf("%d",&int_m);
printf("enter the new elements:");
ptr_b=(int*)realloc(ptr_b,int_n+int_m);//create realloc memory
for(int_j=int_n;int_j<(int_n+int_m);int_j++)//check condition using loop
{
scanf("%d",ptr_b+int_j);
}
for(int_i=0;int_i<(int_n+int_m);++int_i)
{
printf("%d",*(ptr_b+int_i));//print the value
}
}
getch();
free(ptr_b);//clear the allocated memory
return 0;
}
OUT PUT
Enter number of element
3
Now,enter the element
4
5
6
Do you want to enter more numbers?.y/n
Y
Enter the number of extra elements
2
Enter the new element
8
9
45689
THANK YOU
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

PPTX
Pointers,virtual functions and polymorphism cpp
PPT
pre processor directives in C
PPT
constants, variables and datatypes in C
PPTX
Chap6 procedures &amp; macros
PPTX
Function in c program
PPT
Variables in C Programming
PPTX
inline function
PDF
Preprocessor
Pointers,virtual functions and polymorphism cpp
pre processor directives in C
constants, variables and datatypes in C
Chap6 procedures &amp; macros
Function in c program
Variables in C Programming
inline function
Preprocessor

What's hot (20)

DOC
C fundamental
PPTX
Dynamic memory allocation
PPTX
Exception handling c++
PPTX
Operator overloading
PPTX
Pointers, virtual function and polymorphism
PPTX
Character set of c
PPTX
C tokens
PPTX
Function C programming
PPT
Pointers C programming
PPT
PDF
C programming notes
PPTX
C programing -Structure
PPTX
Pointers in c language
PPT
Introduction to C Programming - I
PPTX
Structure & union
PPTX
Functions in C.pptx
PPTX
Unit iv(simple code generator)
PPT
Structure in C
PPTX
file pointers & manipulators.pptx
C fundamental
Dynamic memory allocation
Exception handling c++
Operator overloading
Pointers, virtual function and polymorphism
Character set of c
C tokens
Function C programming
Pointers C programming
C programming notes
C programing -Structure
Pointers in c language
Introduction to C Programming - I
Structure & union
Functions in C.pptx
Unit iv(simple code generator)
Structure in C
file pointers & manipulators.pptx
Ad

Viewers also liked (9)

PPSX
4 dynamic memory allocation
PPTX
16 dynamic-memory-allocation
PPTX
C dynamic ppt
PPT
File in c
PPTX
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
PPTX
File handling in C
PPTX
File handling in c
PPTX
Dynamic memory allocation in c++
PPSX
INTRODUCTION TO C PROGRAMMING
4 dynamic memory allocation
16 dynamic-memory-allocation
C dynamic ppt
File in c
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
File handling in C
File handling in c
Dynamic memory allocation in c++
INTRODUCTION TO C PROGRAMMING
Ad

Similar to Memory allocation in c (20)

PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
Dynamic Memory Allocation in C
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PPTX
Dynamic Memory Allocation(DMA)
PPTX
dynamic_v1-3.pptx
DOCX
DS UNIT3_LINKED LISTS.docx
PPTX
Dynamic Memory allocation
PPT
Dynamic Memory Allocation
DOCX
Dma
PPTX
final GROUP 4.pptx
PPTX
Dynamic memory Allocation in c language
PDF
dynamic-allocation.pdf
PPTX
Memory Management.pptx
PDF
Data Structure - Dynamic Memory Allocation
PPTX
Dynamic memory allocation
PDF
Dynamic memory allocation
PPTX
Dynamic memeory allocation DMA (dyunamic momory .pptx
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
Dynamic Memory Allocation in C
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
CLanguage_ClassPPT_3110003_unit 9Material.ppt
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
Dynamic Memory Allocation(DMA)
dynamic_v1-3.pptx
DS UNIT3_LINKED LISTS.docx
Dynamic Memory allocation
Dynamic Memory Allocation
Dma
final GROUP 4.pptx
Dynamic memory Allocation in c language
dynamic-allocation.pdf
Memory Management.pptx
Data Structure - Dynamic Memory Allocation
Dynamic memory allocation
Dynamic memory allocation
Dynamic memeory allocation DMA (dyunamic momory .pptx

More from Muhammed Thanveer M (20)

DOCX
Easy check is simple and easy use lodge management system
DOCX
KEYBAN...MODULES
PPTX
KeyBan....easy to think and work
PPTX
DOCX
mysql ....question and answer by muhammed thanveer melayi
PPTX
Transation.....thanveeer
PPTX
Stored procedures by thanveer danish melayi
PPTX
Statements,joins and operators in sql by thanveer danish melayi(1)
PPTX
Udf&views in sql...by thanveer melayi
PPTX
Aptitude Questions-2
DOCX
The basics of c programming
PPTX
Preprocesser in c
PPTX
Functions with heap and stack....by thanveer danish
PPTX
Elements of c program....by thanveer danish
PPTX
Aptitude model by thanveer danish
PPTX
Preprocesser in c++ by thanveer danish
PPTX
Data base by thanveer danish
PPTX
Oop concept in c++ by MUhammed Thanveer Melayi
DOCX
Understanding c file handling functions with examples
Easy check is simple and easy use lodge management system
KEYBAN...MODULES
KeyBan....easy to think and work
mysql ....question and answer by muhammed thanveer melayi
Transation.....thanveeer
Stored procedures by thanveer danish melayi
Statements,joins and operators in sql by thanveer danish melayi(1)
Udf&views in sql...by thanveer melayi
Aptitude Questions-2
The basics of c programming
Preprocesser in c
Functions with heap and stack....by thanveer danish
Elements of c program....by thanveer danish
Aptitude model by thanveer danish
Preprocesser in c++ by thanveer danish
Data base by thanveer danish
Oop concept in c++ by MUhammed Thanveer Melayi
Understanding c file handling functions with examples

Recently uploaded (20)

PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Download FL Studio Crack Latest version 2025 ?
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Patient Appointment Booking in Odoo with online payment
PPTX
assetexplorer- product-overview - presentation
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Cost to Outsource Software Development in 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Transform Your Business with a Software ERP System
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Systems & Binary Numbers (comprehensive )
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Download FL Studio Crack Latest version 2025 ?
Wondershare Filmora 15 Crack With Activation Key [2025
Patient Appointment Booking in Odoo with online payment
assetexplorer- product-overview - presentation
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Advanced SystemCare Ultimate Crack + Portable (2025)
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Cost to Outsource Software Development in 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Transform Your Business with a Software ERP System
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Salesforce Agentforce AI Implementation.pdf
Design an Analysis of Algorithms II-SECS-1021-03
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Odoo Companies in India – Driving Business Transformation.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf

Memory allocation in c

  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3. Dynamic memory allocation in C MUHAMMEDTHANVEER.M [email protected] www.facebook.com/ thanveer twitter.com/username in.linkedin.com/in/profilename 9526960445
  • 4. The process of allocating memory during program execution is called dynamic memory allocation. 4 dynamic memory allocation functions S.No Function Syntax 1 malloc () malloc (number *sizeof(int)); 2 calloc () calloc (number, sizeof(int)); 3 realloc () realloc (pointer_name, number * sizeof(int)); 4 free () free (pointer_name);
  • 5. 1. malloc() function in C: malloc () function is used to allocate space in memory during the execution f the program. malloc () does not initialize the memory allocated during execution. It carries garbage value. malloc () function returns null pointer if it couldn’t able to allocate requested amount of memory.
  • 6. EXAMPLE #include <stdio.h> #include <string.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 memoryn"); } else { strcpy( mem_allocation,“[email protected]"); output:[email protected] } printf("Dynamically allocated memory content : " "%sn", mem_allocation ); free(mem_allocation); }
  • 7. 2. calloc() function in C: calloc () function is also like malloc () function. But calloc () initializes the allocated memory to zero. But, malloc() doesn’t.
  • 8. EXAMPLE #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *mem_allocation; /* memory is allocated dynamically */ mem_allocation = calloc( 20, sizeof(char) ); Output:[email protected] if( mem_allocation== NULL ) { printf("Couldn't able to allocate requested memoryn"); } else { strcpy( mem_allocation,“[email protected]"); } printf("Dynamically allocated memory content : " "%sn", mem_allocation ); free(mem_allocation); }
  • 9. 3. realloc() function in C: realloc () function modifies the allocated memory size by malloc () and calloc () functions to new size. If enough space doesn’t exist in memory of current block to extend, new block is allocated for the full size of reallocation, then copies the existing data to new block and then frees the old block. 4. free() function in C: free () function frees the allocated memory by malloc (), calloc (), realloc () functions and returns the memory to the system.
  • 10. EXAMPLE //insert extra numbers #include<stdio.h>//header #include<stdlib.h> int main()//main function { int int_n,int_i,int_j,int_m,*ptr_b;//local variable declaration char str_a; printf("enter number of element");//by user scanf("%d",&int_n);//read and assign ptr_b=(int*)calloc(int_n,sizeof(int));//malloc memory allocation if(ptr_b==NULL) { printf("ERROR!memory not allocated"); exit(0); } printf("Now,enter the element");//by user for(int_i=0;int_i<int_n;++int_i)//check condition { scanf("%d",ptr_b+int_i);//block of statement }
  • 11. printf("do you want to enter more numbers?.y/n"); scanf("%s",&str_a); if(str_a=='y'||str_a=='Y')//check condition using if { printf("enter the number of extra element:"); scanf("%d",&int_m); printf("enter the new elements:"); ptr_b=(int*)realloc(ptr_b,int_n+int_m);//create realloc memory for(int_j=int_n;int_j<(int_n+int_m);int_j++)//check condition using loop { scanf("%d",ptr_b+int_j); } for(int_i=0;int_i<(int_n+int_m);++int_i) { printf("%d",*(ptr_b+int_i));//print the value } } getch(); free(ptr_b);//clear the allocated memory return 0; }
  • 12. OUT PUT Enter number of element 3 Now,enter the element 4 5 6 Do you want to enter more numbers?.y/n Y Enter the number of extra elements 2 Enter the new element 8 9 45689
  • 14. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 15. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: [email protected]