-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_Memory_Allocation.c
More file actions
67 lines (57 loc) · 2.5 KB
/
Dynamic_Memory_Allocation.c
File metadata and controls
67 lines (57 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
#include <stdio.h>
#include <stdlib.h>
Dynamic Memory Allocation(DMA):
It is a way to allocate memory to a data structure during the runtime.
Two types of time :
1) Compile Time
2) Run Time
We need some functions to allocate & free memory dynamically.
Functions for DMA:
a. malloc() : memory allocation
b. calloc() : continuous allocation
c. free() : free function
d. realloc() : re-allocation
a. malloc() : memory allocation
takes number of bytes to be allocated & returns a pointer of type void.
Syntax :
ptr = (Typecast*) malloc(size);
Example :
int *ptr;
ptr = (int*) malloc(10 * sizeof(int)); // Allocating memory for 10 integer
Note :
i. malloc() returns a void pointer which can be cast to any data type.
ii. If memory allocation fails, it returns NULL.
b. calloc() : continuous allocation
Syntax :
ptr = (typeCast*) calloc(total no of location, size);
Example :
int *ptr;
ptr = (int*) calloc(10, sizeof(int)); // Allocating memory for 10 integers
Note :
i. calloc() returns a void pointer which can be cast to any data type.
ii. If memory allocation fails, it returns NULL.
iii. Initializes with 0.
c. free() : free function
We use it to free memory that is allocated using malloc & calloc.
Syntax :
free(ptr);
Example :
int *ptr;
ptr = (int*) malloc(10 * sizeof(int));
// Use the allocated memory
free(ptr); // Free the allocated memory
Note :
i. It is used to free the memory allocated by malloc() or calloc().
d. realloc() : re-allocation
reallocate (increase or decrease ) memory using the same ointer & size.
Syntax :
ptr = (castType*) realloc(ptr, newSize);
Example :
int *ptr;
ptr = (int*) malloc(10 * sizeof(int));
// Use the allocated memory
ptr = (int*) realloc(ptr, 20 * sizeof(int)); // Re-allocating memory for 20 integers
Note :
i. It is used to resize the memory block allocated by malloc() or calloc().
*/