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
Basic Computer Organization and Design
PPTX
Fundamentals of C Programming Language
PPTX
Dynamic memory allocation
PPTX
Preprocessor
PPT
Instruction cycle
PPTX
Memory Hierarchy
PPTX
Register transfer language
PPTX
C programming - String
Basic Computer Organization and Design
Fundamentals of C Programming Language
Dynamic memory allocation
Preprocessor
Instruction cycle
Memory Hierarchy
Register transfer language
C programming - String

What's hot (20)

PPTX
16 dynamic-memory-allocation
PDF
Basic Computer Organization and Design
PPTX
Computer architecture
PPTX
Function in c
PPTX
Register & flags
PPTX
Dynamic memory Allocation in c language
PPTX
Type conversion, precedence, associativity in c programming
PPTX
Paging and segmentation
PPTX
Data types
PDF
Address Binding Scheme
PPTX
C Tokens
PPTX
Demand paging
PPT
Memory allocation (4)
PPTX
Instruction cycle presentation
PDF
Module 05 Preprocessor and Macros in C
PPSX
5bit field
PPTX
Basic Data Types in C++
PPTX
Concept of c data types
PPTX
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
16 dynamic-memory-allocation
Basic Computer Organization and Design
Computer architecture
Function in c
Register & flags
Dynamic memory Allocation in c language
Type conversion, precedence, associativity in c programming
Paging and segmentation
Data types
Address Binding Scheme
C Tokens
Demand paging
Memory allocation (4)
Instruction cycle presentation
Module 05 Preprocessor and Macros in C
5bit field
Basic Data Types in C++
Concept of c data types
Dynamic Programming Code-Optimization Algorithm (Compiler Design)
Ad

Viewers also liked (8)

PPSX
4 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
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
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
PPTX
Dynamic memory allocation
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-allocation.pdf
Memory Management.pptx
Data Structure - Dynamic Memory Allocation
Dynamic memory allocation
Dynamic memory allocation
Dynamic memeory allocation DMA (dyunamic momory .pptx
Dynamic memory allocation

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
PTS Company Brochure 2025 (1).pdf.......
PPTX
Introduction to Artificial Intelligence
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
medical staffing services at VALiNTRY
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PTS Company Brochure 2025 (1).pdf.......
Introduction to Artificial Intelligence
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
How to Choose the Right IT Partner for Your Business in Malaysia
2025 Textile ERP Trends: SAP, Odoo & Oracle
medical staffing services at VALiNTRY
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How Creative Agencies Leverage Project Management Software.pdf
Nekopoi APK 2025 free lastest update
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Navsoft: AI-Powered Business Solutions & Custom Software Development
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
System and Network Administraation Chapter 3
Odoo Companies in India – Driving Business Transformation.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

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 kutubmadar@gmail.com 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,“shernoobi@gmail.com"); output:shernoobi@gmail.com } 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: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); }
  • 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: info@baabtra.com