SlideShare a Scribd company logo
DYNAMIC MEMORY ALLOCATION
DYNAMIC MEMORY ALLOCATION
• Dynamic Memory Allocation is manual allocation and freeing of
memory according to your programming needs.
• Dynamic memory is managed and served with pointers that point to
the newly allocated memory space in an area which we call the heap.
MALLOC() FUNCTION IN C
• The C malloc() function stands for memory allocation. It is a function
which is used to allocate a block of memory dynamically.
• It reserves memory space of specified size and returns the null pointer
pointing to the memory location.
• We can assign C malloc() function to any pointer.
• Syntax of malloc() Function:
ptr = (cast_type *) malloc (byte_size);
Here,
1. ptr is a pointer of cast_type.
2. The C malloc() function returns a pointer to the allocated memory of byte_size.
MALLOC()
• #include <stdlib.h>
int main(){
int *ptr;
ptr = malloc(15 * sizeof(*ptr)); /* a block of 15 integers */
if (ptr != NULL) {
*(ptr + 5) = 480; /* assign 480 to sixth integer */
printf("Value of the 6th integer is %d",*(ptr + 5));
}
}
• Output: Value of the 6th integer is 480
CALLOC() FUNCTION IN C
• The C calloc() function stands for contiguous allocation. This function is
used to allocate multiple blocks of memory.
• It is a dynamic memory allocation function which is used to allocate the
memory to complex data structures such as arrays and structures.
• Syntax of calloc() Function:
ptr = (cast_type *) calloc (n, size);
• The above statement is used to allocate n memory blocks of the same size.
• After the memory space is allocated, then all the bytes are initialized to
zero.
• The pointer which is currently at the first byte of the allocated memory
space is returned.
• #include <stdio.h>
• int main() {
• int i, * ptr, sum = 0;
• ptr = calloc(10, sizeof(int));
• if (ptr == NULL) {
• printf("Error! memory not allocated.");
• exit(0);
• }
• printf("Building and calculating the sequence sum of the first 10 terms  n ");
• for (i = 0; i < 10; ++i) { * (ptr + i) = i;
• sum += * (ptr + i);
• }
• printf("Sum = %d", sum);
• free(ptr);
REALLOC() FUNCTION IN C
• Using the C realloc() function, you can add more memory size to already
allocated memory.
• It expands the current block while leaving the original content as it is.
realloc() in C stands for reallocation of memory.
• realloc() can also be used to reduce the size of the previously allocated
memory.
• Syntax of realloc() Function:
ptr = realloc (ptr,newsize);
• The above statement allocates a new memory space with a specified size in
the variable newsize.
• After executing the function, the pointer will be returned to the first byte of
the memory block. The new size can be larger or smaller than the previous
memory.
• #include <stdio.h>
• int main () {
• char *ptr;
• ptr = (char *) malloc(10);
• strcpy(ptr, "Programming");
• printf(" %s, Address = %un", ptr, ptr);
•
• ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
• strcat(ptr, " In 'C'");
• printf(" %s, Address = %un", ptr, ptr);
• free(ptr);
• return 0;
• }
FREE() FUNCTION IN C
• The memory for variables is automatically deallocated at compile
time.
• In dynamic memory allocation, you have to deallocate memory
explicitly.
• If not done, you may encounter out of memory error.
• The free() function is called to release/deallocate memory in C.
• By freeing memory in your program, you make more available for use
later.
FREE()
• #include <stdio.h>
• int main() {
• int* ptr = malloc(10 * sizeof(*ptr));
• if (ptr != NULL){
• *(ptr + 2) = 50;
• printf("Value of the 2nd integer is %d",*(ptr + 2));
• }
• free(ptr);
• }
• Output: Value of the 2nd integer is 50
CALLOC() VS. MALLOC()
• The calloc() function is generally more suitable and efficient than that
of the malloc() function.
• While both the functions are used to allocate memory space, calloc()
can allocate multiple blocks at a single time. You don’t have to request
for a memory block every time.
• The calloc() function is used in complex data structures which require
larger memory space.
• The memory block allocated by a calloc() in C is always initialized to
zero while in function malloc() in C, it always contains a garbage
value.

More Related Content

PPTX
CS304PC:Computer Organization and Architecture Session 28 Direct memory acces...
PPTX
Presentation on Shared Memory Parallel Programming
PPTX
Python oop - class 2 (inheritance)
PPTX
Friend function
PPTX
Malloc() and calloc() in c
PDF
C++ OOPS Concept
DOCX
Recursion in C++
PPTX
Fragmentaton
CS304PC:Computer Organization and Architecture Session 28 Direct memory acces...
Presentation on Shared Memory Parallel Programming
Python oop - class 2 (inheritance)
Friend function
Malloc() and calloc() in c
C++ OOPS Concept
Recursion in C++
Fragmentaton

What's hot (20)

PPT
java programming - applets
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
PPTX
contiguous memory allocation.pptx
PPT
Memory allocation in c
PPT
File handling
PPT
Object-oriented concepts
PPTX
Macro and Preprocessor in c programming
PPT
Basic concepts of oops
PPTX
Transaction Processing in DBMS.pptx
PPT
Classical problem of synchronization
PPTX
queue & its applications
PPTX
Dynamic Memory Allocation(DMA)
PPTX
Python-Classes.pptx
PPTX
Tree traversal techniques
PPTX
PDF
Python twisted
PPTX
File concept and access method
PPT
Greedy method by Dr. B. J. Mohite
PPT
Disk scheduling algorithms
PPTX
Types of function call
java programming - applets
INTER PROCESS COMMUNICATION (IPC).pptx
contiguous memory allocation.pptx
Memory allocation in c
File handling
Object-oriented concepts
Macro and Preprocessor in c programming
Basic concepts of oops
Transaction Processing in DBMS.pptx
Classical problem of synchronization
queue & its applications
Dynamic Memory Allocation(DMA)
Python-Classes.pptx
Tree traversal techniques
Python twisted
File concept and access method
Greedy method by Dr. B. J. Mohite
Disk scheduling algorithms
Types of function call
Ad

Similar to DYNAMIC MEMORY ALLOCATION.pptx (20)

PPTX
PF UE LEC 7 Pointers programming fundamentals (2).pptx
PPTX
Dynamic memory allocation
PPTX
Dynamic Memory allocation
PPTX
dynamic_v1-3.pptx
PPTX
Memory Management.pptx
PDF
Dynamic memory allocation
PPTX
pointers in c programming - example programs
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PDF
Data Structure - Dynamic Memory Allocation
PPTX
Dynamic memory allocation
PDF
Programming in C language specially allocation
PDF
dynamic-allocation.pdf
PPTX
Dynamic Memory Allocation.pptx for c language and basic knowledge.
PPTX
Dynamic Memory Allocation in C
PPTX
Dynamic memory allocation in c
PPTX
PPTX
dynamicmemoryallocation.pptx
PPTX
Memory management CP
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
PF UE LEC 7 Pointers programming fundamentals (2).pptx
Dynamic memory allocation
Dynamic Memory allocation
dynamic_v1-3.pptx
Memory Management.pptx
Dynamic memory allocation
pointers in c programming - example programs
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
Data Structure - Dynamic Memory Allocation
Dynamic memory allocation
Programming in C language specially allocation
dynamic-allocation.pdf
Dynamic Memory Allocation.pptx for c language and basic knowledge.
Dynamic Memory Allocation in C
Dynamic memory allocation in c
dynamicmemoryallocation.pptx
Memory management CP
CLanguage_ClassPPT_3110003_unit 9Material.ppt
Ad

More from LECO9 (20)

PPT
C Programming Intro.ppt
PPTX
Embedded Systems.pptx
PPTX
Basic Electronics.pptx
PPTX
Intro to Microcontroller.pptx
PPTX
PIC_Intro.pptx
PPTX
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
PPTX
STACKS AND QUEUES.pptx
PPTX
UNIONS IN C.pptx
PPTX
Processes, Threads.pptx
PPTX
OPERATORS IN C.pptx
PPTX
DATA STRUCTURES AND LINKED LISTS IN C.pptx
PPTX
FUNCTIONS IN C.pptx
PPTX
DESIGN PATTERN.pptx
PPTX
cprogramming Structures.pptx
PPTX
POINTERS.pptx
PPTX
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
PPTX
cprogramming strings.pptx
PPTX
C-Programming Control statements.pptx
PPTX
C-Programming Function pointers.pptx
PPTX
C-Programming File-handling-C.pptx
C Programming Intro.ppt
Embedded Systems.pptx
Basic Electronics.pptx
Intro to Microcontroller.pptx
PIC_Intro.pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
STACKS AND QUEUES.pptx
UNIONS IN C.pptx
Processes, Threads.pptx
OPERATORS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
FUNCTIONS IN C.pptx
DESIGN PATTERN.pptx
cprogramming Structures.pptx
POINTERS.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
cprogramming strings.pptx
C-Programming Control statements.pptx
C-Programming Function pointers.pptx
C-Programming File-handling-C.pptx

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PPT on Performance Review to get promotions
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Welding lecture in detail for understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
composite construction of structures.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Foundation to blockchain - A guide to Blockchain Tech
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Digital Logic Computer Design lecture notes
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Structs to JSON How Go Powers REST APIs.pdf
bas. eng. economics group 4 presentation 1.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Lecture Notes Electrical Wiring System Components
PPT on Performance Review to get promotions
UNIT 4 Total Quality Management .pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Welding lecture in detail for understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
composite construction of structures.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx

DYNAMIC MEMORY ALLOCATION.pptx

  • 2. DYNAMIC MEMORY ALLOCATION • Dynamic Memory Allocation is manual allocation and freeing of memory according to your programming needs. • Dynamic memory is managed and served with pointers that point to the newly allocated memory space in an area which we call the heap.
  • 3. MALLOC() FUNCTION IN C • The C malloc() function stands for memory allocation. It is a function which is used to allocate a block of memory dynamically. • It reserves memory space of specified size and returns the null pointer pointing to the memory location. • We can assign C malloc() function to any pointer. • Syntax of malloc() Function: ptr = (cast_type *) malloc (byte_size); Here, 1. ptr is a pointer of cast_type. 2. The C malloc() function returns a pointer to the allocated memory of byte_size.
  • 4. MALLOC() • #include <stdlib.h> int main(){ int *ptr; ptr = malloc(15 * sizeof(*ptr)); /* a block of 15 integers */ if (ptr != NULL) { *(ptr + 5) = 480; /* assign 480 to sixth integer */ printf("Value of the 6th integer is %d",*(ptr + 5)); } } • Output: Value of the 6th integer is 480
  • 5. CALLOC() FUNCTION IN C • The C calloc() function stands for contiguous allocation. This function is used to allocate multiple blocks of memory. • It is a dynamic memory allocation function which is used to allocate the memory to complex data structures such as arrays and structures. • Syntax of calloc() Function: ptr = (cast_type *) calloc (n, size); • The above statement is used to allocate n memory blocks of the same size. • After the memory space is allocated, then all the bytes are initialized to zero. • The pointer which is currently at the first byte of the allocated memory space is returned.
  • 6. • #include <stdio.h> • int main() { • int i, * ptr, sum = 0; • ptr = calloc(10, sizeof(int)); • if (ptr == NULL) { • printf("Error! memory not allocated."); • exit(0); • } • printf("Building and calculating the sequence sum of the first 10 terms n "); • for (i = 0; i < 10; ++i) { * (ptr + i) = i; • sum += * (ptr + i); • } • printf("Sum = %d", sum); • free(ptr);
  • 7. REALLOC() FUNCTION IN C • Using the C realloc() function, you can add more memory size to already allocated memory. • It expands the current block while leaving the original content as it is. realloc() in C stands for reallocation of memory. • realloc() can also be used to reduce the size of the previously allocated memory. • Syntax of realloc() Function: ptr = realloc (ptr,newsize); • The above statement allocates a new memory space with a specified size in the variable newsize. • After executing the function, the pointer will be returned to the first byte of the memory block. The new size can be larger or smaller than the previous memory.
  • 8. • #include <stdio.h> • int main () { • char *ptr; • ptr = (char *) malloc(10); • strcpy(ptr, "Programming"); • printf(" %s, Address = %un", ptr, ptr); • • ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size • strcat(ptr, " In 'C'"); • printf(" %s, Address = %un", ptr, ptr); • free(ptr); • return 0; • }
  • 9. FREE() FUNCTION IN C • The memory for variables is automatically deallocated at compile time. • In dynamic memory allocation, you have to deallocate memory explicitly. • If not done, you may encounter out of memory error. • The free() function is called to release/deallocate memory in C. • By freeing memory in your program, you make more available for use later.
  • 10. FREE() • #include <stdio.h> • int main() { • int* ptr = malloc(10 * sizeof(*ptr)); • if (ptr != NULL){ • *(ptr + 2) = 50; • printf("Value of the 2nd integer is %d",*(ptr + 2)); • } • free(ptr); • } • Output: Value of the 2nd integer is 50
  • 11. CALLOC() VS. MALLOC() • The calloc() function is generally more suitable and efficient than that of the malloc() function. • While both the functions are used to allocate memory space, calloc() can allocate multiple blocks at a single time. You don’t have to request for a memory block every time. • The calloc() function is used in complex data structures which require larger memory space. • The memory block allocated by a calloc() in C is always initialized to zero while in function malloc() in C, it always contains a garbage value.