Dynamic Memory Allocation PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document explains dynamic memory allocation in C. It describes the functions malloc, calloc, free, and realloc for allocating and freeing memory during runtime. The document provides a concise overview of these functions and their intended use within C programming.
Full Transcript
Dynamic Memory Allocation It is a way to allocate memory to a data structure during the runtime. We need some functions to allocate & free memory dynamically. Functions for DMA a. malloc( ) b. calloc( ) c. free( ) d. realloc( ) malloc( ) memory allocation takes number of bytes to be allocated...
Dynamic Memory Allocation It is a way to allocate memory to a data structure during the runtime. We need some functions to allocate & free memory dynamically. Functions for DMA a. malloc( ) b. calloc( ) c. free( ) d. realloc( ) malloc( ) memory allocation takes number of bytes to be allocated & returns a pointer of type void ptr = (*int) malloc(5 * sizeof(int)); calloc( ) continuous allocation initializes with 0 ptr = (*int) calloc(5, sizeof(int)); free( ) We use it to free memory that is allocated using malloc & calloc free(ptr); realloc( ) reallocate (increase or decrease) memory using the same pointer & size. ptr = realloc(ptr, newSize);