Dynamic memory allocation
C prog
Dynamic memory allocation When we declare a variable using a basic data
type, the C compiler automatically allocates
memory space for the variable in a pool of
memory called the stack.
For example, a float variable
takes 4 bytes in stack
#include <stdio.h>
int main() An array with a specified size is allocated in
contiguous blocks of memory ,the size of
{
this array is decide at design time .
float arr[10];
printf("The size of the float array is %d", sizeof(arr));
return 0;
}
when we declare a basic data type or an array,
the memory is automatically managed.
There is a process for allocating memory in C
which will permit to implement array size is
undecided until you run your program (runtime).
This process is called “Dynamic memory
allocation.”
Dynamic Memory Allocation in C
Dynamic Memory Allocation is manual allocation
and freeing of memory according to your
programming needs.
Dynamic memory is managed with pointers that
point to the newly allocated memory space in an
area which we call the heap.
We can create and destroy an array of elements dynamically at
runtime, the Automatic memory management uses the stack,
and the C Dynamic Memory Allocation uses the heap.
The <stdlib.h> library has functions responsible for Dynamic
Memory Management.
Function Purpose
Allocates the memory of requested
malloc() size and returns the pointer to the
first byte of allocated space.
Allocates the space for elements of
an array. Initializes the elements to
calloc() zero and returns a pointer to the
memory.
It is umodify the size of previously
realloc() allocated memory spacesed to.
Frees or empties the previously
Free() allocated memory space.
malloc() The C malloc() function stands for memory allocation.
function
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.
The pointer returned is usually of type void. It means that
we can assign C malloc() function to any pointer.
Syntax :
ptr = (cast_type *) malloc (byte_size);
Example of malloc():
ptr = (int *) malloc (40);
When this statement is successfully executed, a
memory space of 40 bytes is reserved.
The address of the first byte of reserved space is
assigned to the pointer ptr of type int.
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int*)malloc(4); /* a block of 1 integer */
if (ptr != NULL)
{
*(ptr) = 480; /* assign 480 to 1st integer */
printf("Value of the 1st integer is %d",*(ptr));
}
}
#include <stdlib.h>
int main()
{
int *ptr;
int i;
ptr = malloc(sizeof(int)); /* a block of 1 integer */
if (ptr != NULL)
{
*(ptr) = 480; /* assign 480 to 1st integer */
printf("Value of the 1st integer is %d",*(ptr));
}
}
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int*)malloc(2*4); /* a block of 2 integers */
if (ptr != NULL)
{
*(ptr+1) = 500; /* assign 500 to 2nd integer */
printf("Value of the 2nd integer is %d",*(ptr+1));
}
}
free() function 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(ptr);
#include <stdlib.h>
struct node {
int data;
struct node *next;
}; head = n1;
n1->data = 1;
void main() n1->next = n2;
{ n2->data = 2;
n2->next = n3;
struct node *head = NULL; n3->data = 3;
struct node *n1, *n2, *n3; n3->next = NULL; /* <-- indicates end of list */
n1 = (struct node *) malloc(sizeof(struct node));
n2 = (struct node *) malloc(sizeof(struct node)); printf("\nValue of 1st node data = %d",head->data);
n3 = (struct node *) malloc(sizeof(struct node)); printf("\nValue of 1st node data = %d",n1->data);
printf("\nValue of 1st node data = %d",n2->data);
printf("\nValue of 1st node data = %d",n3->data);
}
#include <stdlib.h>
struct node { head = n1;
int data; n1->data = 1;
struct node *next; n1->next = n2;
}; n2->data = 2;
n2->next = n3;
void main() n3->data = 3;
{ n3->next = NULL; /* <-- indicates end of list */
int i=1;
struct node *head = NULL; tempptr=head;
struct node *n1, *n2, *n3,*tempptr;
n1 = (struct node *) malloc(sizeof(struct node)); while (tempptr != NULL)
n2 = (struct node *) malloc(sizeof(struct node)); {
n3 = (struct node *) malloc(sizeof(struct node)); printf("\nValue of %d node data = %d",i,tempptr->data);
tempptr=tempptr->next;
} i++;
}
#include <stdlib.h>
struct course {
int marks;
char subject[30]; for(i=0;i<noOfRecords;i++)
}; {
printf("Enter subject and marks:\n");
int main() scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);
{ }
struct course *ptr;
int noOfRecords,i; printf("Displaying Information:\n");
printf("Enter the number of records: "); for (i = 0; i < noOfRecords; ++i)
scanf("%d",&noOfRecords); {
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
/* Memory allocation for noOfRecords structures*/ }
ptr=(struct course *)malloc(noOfRecords*sizeof(struct course));
free(ptr);
return 0;
}