0% found this document useful (0 votes)
28 views

Dynamic Memory Allocation

Dynamic memory allocation allows programs to request additional memory from the operating system at runtime using functions like malloc(), calloc(), and realloc() rather than being restricted to static pre-allocated memory. These functions allocate memory blocks and return pointers that can be assigned to variables, with malloc() allocating uninitialized memory and calloc() allocating initialized memory. Programs must free allocated memory blocks using free() to avoid memory leaks.

Uploaded by

Rahul Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Dynamic Memory Allocation

Dynamic memory allocation allows programs to request additional memory from the operating system at runtime using functions like malloc(), calloc(), and realloc() rather than being restricted to static pre-allocated memory. These functions allocate memory blocks and return pointers that can be assigned to variables, with malloc() allocating uninitialized memory and calloc() allocating initialized memory. Programs must free allocated memory blocks using free() to avoid memory leaks.

Uploaded by

Rahul Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Dynamic Memory Allocation

Dynamic memory allocation allows your


program to obtain more memory space
while running, or to release it if it's not
required.
Dynamic Memory Allocation
• 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.
Dynamic Memory Allocation
Function Use of Function

malloc() Allocates requested size of bytes and returns a


pointer to the 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


malloc()
• malloc() function is used for allocating block
of memory at runtime. This function reserves
a block of memory of given size and returns a
pointer of type void. This means that we can
assign it to any type of pointer using
typecasting. If it fails to locate enough space it
returns a NULL pointer.
malloc()
ptr = (cast-type*) malloc(byte-size)

int *x; x = (int*)malloc(50 * sizeof(int));


//memory space allocated to variable x
free(x);
calloc()
• calloc() is another memory allocation function
that is used for allocating memory at
runtime. calloc function is normally used for
allocating memory to derived data types such
as arrays and structures. If it fails to locate
enough space it returns a NULL pointer.
• The name calloc stands for "contiguous
allocation".
calloc()
(cast_type *)calloc(blocks , size_of_block);
struct employee
{ char *name;
int salary;
};
typedef struct employee emp;
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));
Difference between malloc() and calloc()

calloc() malloc()
calloc() initializes the allocated memory malloc() initializes the allocated memory
with 0 value. with garbage values.

Number of arguments is 2 Number of argument is 1

Syntax : Syntax :
(cast_type *)calloc(blocks , size_of_block); (cast_type *)malloc(Size_in_bytes);

Malloc does its job quickly. Calloc is slow in comparison to Malloc.


Contiguous Allocation is the full form of Memory Allocation is the full form of
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()
free(ptr);
realloc()

• If the previously allocated memory is


insufficient or more than required, you can
change the previously allocated memory size
using realloc().
• ptr = realloc(ptr, newsize);
Limitation of Static Arrays
• Inability to resize an array at run-time:As the
memory for arrays is allocated at compile
time, it is not possible to change the array size
during program execution. Thus, if the
declared array size is insufficient in some
situation, the executable program is almost
useless. In such situations, we need to modify
the source code suitably and recompile the
program.
Limitation of Static Arrays
• Memory wastage: As array size cannot be
increased at run-time, a programmer tends to
declare arrays much larger than usually
required to cater to some unforeseen
situations. This leads to wastage of memory in
most of the executions.
dynamic arrays
• To overcome the limitations of static arrays ,
we can implement the dynamic arrays using
the dynamic memory allocation feature of the
C language. This allows a programmer to
allocate only the required memory for arrays.
• The C programming language does not
have dynamic array as a language feature
Steps to create a dynamic array
Create a pointer variable.
For ex:- double* p;
Allocate memory cells for the array
elements and make p point to the first array
element:
p=(double *) calloc(10,sizeof(double);
Dynamic array
double *p;
p = calloc(10, sizeof(double) );

int* array;
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
array = (int*) calloc(n,sizeof(int));
Dynamic Array
Steps for creating 2d Array using Pointer.
• Create a pointer to pointer and allocate the
memory for the row using malloc().
1 for(i = 0; i < nrows; i++)
2 {
3 piBuffer[i] = malloc( ncolumns * sizeof(int));
4 }
Dynamic Arrays
• Allocate memory for each row columns using
the malloc().

1 for(i = 0; i < nrows; i++)


2 {
3 piBuffer[i] = malloc( ncolumns * sizeof(int));
4 }
2D Arrays

You might also like