2. Why we need Dynamic Memory?
The main disadvantage of the array is that size of the array
is fixed; it means we cannot change the size of the array at
run time. In other words we can say that there is no option
to expand or shrink the size of the array. To overcome
this and disadvantage we need a solution like dynamic
memory in which the memory is created; expand and
shrink at runtime as per requirement
3. malloc()
"malloc" full form is memory allocation.
The malloc() function reserves a block of memory of the
specified number of bytes. And, it returns a pointer of void
which can be casted into pointers of any form.
5. #include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %dn", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
6. calloc()
calloc" stands for contiguous allocation.
The malloc() function allocates memory and leaves the
memory uninitialized, whereas the calloc() function
allocates memory and initializes all bits to zero.
8. #include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %dn", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
9. realloc()
If the dynamically allocated memory is insufficient or more
than required, you can change the size of previously
allocated memory using the realloc() function.
11. int main()
{
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %dn", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.n");
exit(0); }
else {
printf("Memory successfully allocated using calloc.n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);}
n = 10;
printf("nnEnter the new size of the array: %dn", n);
ptr = realloc(ptr, n * sizeof(int));
printf("Memory successfully re-allocated using realloc.n");
for (i = 5; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
free(ptr); }
return 0; }
12. free()
Dynamically allocated memory created with either calloc()
or malloc() doesn't get freed on their own. You must
explicitly use free() to release the space.
free(ptr);
Syntax
13. new operator
The new operator denotes a request for memory allocation
on the Free Store. If sufficient memory is available, new
operator initializes the memory and returns the address of
the newly allocated and initialized memory to the pointer
variable.
pointer-variable = new data-type;
Syntax
14. Allocate block of memory:
› new operator is also used to allocate a block(an array) of
memory of type data-type.
pointer-variable = new data-type[size];
Syntax
Example
int *p = new int[10]
15. delete operator
› Delete is an operator that is used to destroy array and
non-array(pointer) objects which are created by new
expression.
delete pointer-variable;
Syntax