SlideShare a Scribd company logo
1 of 14
Gandhinagar Institute Of Technology
 Subject :-Computer Programming And Utilizaton ()
 Topic :- Dynamic Memory Allocation
 Branch :- Computer Engineering (CE)
 Division :- B
 Prepared By :- Grishma Rajput(160120107122)
 Submitted To :- Prof. Mihir Shah
Dynamic Memory Allocation
 In C, the exact size of array is unknown until compile time, i.e.,
the time when a compiler compiles your code into a computer
understandable language. So, sometimes the size of the array
can be insufficient or more than required.
 Dynamic memory allocation allows your program to obtain more
memory space while running, or to release it if it's not required.
 In simple terms, Dynamic memory allocation allows you to
manually handle memory space for your program.
 Although, C language inherently does not have any technique to
allocate memory dynamically, there are 4 library
functions under "stdlib.h" for dynamic memory allocation.
Memory Allocation Functions
Function Use of Function
malloc()
Allocates requested size of bytes and returns a pointer first
byte of allocated space
calloc()
Allocates space for an array elements, initializes to zero
and then returns a pointer to memory
free() deallocate the previously allocated space
realloc() Change the size of previously allocated space
Memory Allocation Process
 Global variables, static variabl
es and program instructions get
their memory
in permanent storage area
whereas local variables are
stored in area called Stack.
 The memory space between
these two region is known
as Heap area. This region is
used for dynamic memory
allocation during execution of
the program. The size of heap
keep changing.
malloc()
 The name malloc stands for "memory allocation".
 The function malloc () reserves a block of memory of specified size and return
a pointer of type void which can be casted into pointer of any form.
 Syntax of malloc ()
 Here, ptr is pointer of cast-type. The malloc () function returns a pointer to an
area of memory with size of byte size. If the space is insufficient, allocation fails
and returns NULL pointer.
 This statement will allocate either 200 or 400 according to size of int 2 or 4
bytes respectively and the pointer points to the address of first byte of memory
ptr = (cast-type*) malloc(byte-size)
ptr = (int*) malloc(100 * sizeof(int));
malloc ( ) Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main ( )
{
int*p;
clrscr( );
p= (int*) malloc(1* sizeof ( int ));
*p=10;
printf(“n Value: %d”, *p);
free(p);
getch( );
}
OUT PUT:
Value : 10
calloc()
 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.
 Syntax of calloc()
 This statement will allocate contiguous space in memory for an array
of nelements. For example:
 This statement allocates contiguous space in memory for an array of 25
elements each of size of float, i.e, 4 bytes.
ptr = (float*) calloc(25, sizeof(float));
ptr = (cast-type*)calloc(n, element-size);
calloc ( ) Example
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main ( )
{
int*p;
clrscr( );
p= (int*) calloc(1, sizeof ( int ));
*p=10;
printf(“n Value: %d”, *p);
free(p);
getch( );
}
OUT PUT:
Value : 10
Difference between malloc() and calloc()
calloc() malloc()
calloc() initializes the allocated memory with
0 value.
malloc() initializes the allocated memory with
garbage values.
Number of arguments is 2 Number of argument is 1
Syntax :
(cast_type *)calloc(blocks , size_of_block);
Syntax :
(cast_type *)malloc(Size_in_bytes);
C free()
 Dynamically allocated memory created with either calloc() or
malloc() doesn't get freed on its own. You must explicitly use
free() to release the space.
 syntax of free()
 This statement frees the space allocated in the memory pointed
by ptr.
free(ptr);
C realloc()
 If the previously allocated memory is insufficient or more than
required, you can change the previously allocated memory
size using realloc().
 Syntax of realloc()
 This function allocates a new memory space of size new size
to the pointer variable ptr and returns a pointer to the first byte
of the new memory block. The new size may be larger or
smaller than the size.
 If the function is unsucessfull in locating additional space, it
returns NULL pointer and the original block is freed.
ptr = realloc(ptr, newsize);
relloc ( ) Incorrect Eample
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{
int arr[2], i;
int *ptr = arr;
int *ptr_new;
arr[0] = 10;
arr[1] = 20;
// incorrect use of new_ptr: undefined behaviour
ptr_new = (int *) realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
getchar();
}
OUT PUT:
NULL
relloc( ) Correct Example
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *) malloc(sizeof(int)*2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *) realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++)
printf("%d ", *(ptr new + i));
getchar();
}
OUT PUT :
10 20 30
Thank you

More Related Content

What's hot (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Strings
StringsStrings
Strings
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers
PointersPointers
Pointers
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Pointers
PointersPointers
Pointers
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 

Similar to Dynamic Memory allocation

Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptxBilalImran17
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxLECO9
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxSKUP1
 
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 allocationGem WeBlog
 
Dma
DmaDma
DmaAcad
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationMohammad Usman
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxssuser688516
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocationFrijo Francis
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation웅식 전
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in cMahesh Tibrewal
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-stringsNico Ludwig
 

Similar to Dynamic Memory allocation (20)

dynamic_v1-3.pptx
dynamic_v1-3.pptxdynamic_v1-3.pptx
dynamic_v1-3.pptx
 
Dma
DmaDma
Dma
 
Memory Management.pptx
Memory Management.pptxMemory Management.pptx
Memory Management.pptx
 
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
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Stack & heap
Stack & heap Stack & heap
Stack & heap
 
Dma
DmaDma
Dma
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
 
4 dynamic memory allocation
4 dynamic memory allocation4 dynamic memory allocation
4 dynamic memory allocation
 
Linked list
Linked listLinked list
Linked list
 
final GROUP 4.pptx
final GROUP 4.pptxfinal GROUP 4.pptx
final GROUP 4.pptx
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
15 Jo P Mar 08
15 Jo P Mar 0815 Jo P Mar 08
15 Jo P Mar 08
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
Malloc() and calloc() in c
Malloc() and calloc() in cMalloc() and calloc() in c
Malloc() and calloc() in c
 
(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings(5) cpp dynamic memory_arrays_and_c-strings
(5) cpp dynamic memory_arrays_and_c-strings
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 

Recently uploaded

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Recently uploaded (20)

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

Dynamic Memory allocation

  • 1. Gandhinagar Institute Of Technology  Subject :-Computer Programming And Utilizaton ()  Topic :- Dynamic Memory Allocation  Branch :- Computer Engineering (CE)  Division :- B  Prepared By :- Grishma Rajput(160120107122)  Submitted To :- Prof. Mihir Shah
  • 2. Dynamic Memory Allocation  In C, the exact size of array is unknown until compile time, i.e., the time when a compiler compiles your code into a computer understandable language. So, sometimes the size of the array can be insufficient or more than required.  Dynamic memory allocation allows your program to obtain more memory space while running, or to release it if it's not required.  In simple terms, Dynamic memory allocation allows you to manually handle memory space for your program.  Although, C language inherently does not have any technique to allocate memory dynamically, there are 4 library functions under "stdlib.h" for dynamic memory allocation.
  • 3. Memory Allocation Functions Function Use of Function malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory free() deallocate the previously allocated space realloc() Change the size of previously allocated space
  • 4. Memory Allocation Process  Global variables, static variabl es and program instructions get their memory in permanent storage area whereas local variables are stored in area called Stack.  The memory space between these two region is known as Heap area. This region is used for dynamic memory allocation during execution of the program. The size of heap keep changing.
  • 5. malloc()  The name malloc stands for "memory allocation".  The function malloc () reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.  Syntax of malloc ()  Here, ptr is pointer of cast-type. The malloc () function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.  This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory ptr = (cast-type*) malloc(byte-size) ptr = (int*) malloc(100 * sizeof(int));
  • 6. malloc ( ) Example #include<stdio.h> #include<conio.h> #include<stdlib.h> int main ( ) { int*p; clrscr( ); p= (int*) malloc(1* sizeof ( int )); *p=10; printf(“n Value: %d”, *p); free(p); getch( ); } OUT PUT: Value : 10
  • 7. calloc()  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.  Syntax of calloc()  This statement will allocate contiguous space in memory for an array of nelements. For example:  This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes. ptr = (float*) calloc(25, sizeof(float)); ptr = (cast-type*)calloc(n, element-size);
  • 8. calloc ( ) Example #include<stdio.h> #include<conio.h> #include<stdlib.h> int main ( ) { int*p; clrscr( ); p= (int*) calloc(1, sizeof ( int )); *p=10; printf(“n Value: %d”, *p); free(p); getch( ); } OUT PUT: Value : 10
  • 9. Difference between malloc() and calloc() calloc() malloc() calloc() initializes the allocated memory with 0 value. malloc() initializes the allocated memory with garbage values. Number of arguments is 2 Number of argument is 1 Syntax : (cast_type *)calloc(blocks , size_of_block); Syntax : (cast_type *)malloc(Size_in_bytes);
  • 10. C free()  Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space.  syntax of free()  This statement frees the space allocated in the memory pointed by ptr. free(ptr);
  • 11. C realloc()  If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc().  Syntax of realloc()  This function allocates a new memory space of size new size to the pointer variable ptr and returns a pointer to the first byte of the new memory block. The new size may be larger or smaller than the size.  If the function is unsucessfull in locating additional space, it returns NULL pointer and the original block is freed. ptr = realloc(ptr, newsize);
  • 12. relloc ( ) Incorrect Eample #include <stdio.h> #include<conio.h> #include <stdlib.h> int main() { int arr[2], i; int *ptr = arr; int *ptr_new; arr[0] = 10; arr[1] = 20; // incorrect use of new_ptr: undefined behaviour ptr_new = (int *) realloc(ptr, sizeof(int)*3); *(ptr_new + 2) = 30; for(i = 0; i < 3; i++) printf("%d ", *(ptr_new + i)); getchar(); } OUT PUT: NULL
  • 13. relloc( ) Correct Example #include <stdio.h> #include<conio.h> #include <stdlib.h> int main() { int *ptr = (int *) malloc(sizeof(int)*2); int i; int *ptr_new; *ptr = 10; *(ptr + 1) = 20; ptr_new = (int *) realloc(ptr, sizeof(int)*3); *(ptr_new + 2) = 30; for(i = 0; i < 3; i++) printf("%d ", *(ptr new + i)); getchar(); } OUT PUT : 10 20 30