0% found this document useful (0 votes)
136 views3 pages

Dynamic Memory Allocation

The document discusses dynamic memory allocation in C using malloc() and calloc(). It provides sample code to demonstrate allocating memory for a single variable using malloc() and for multiple blocks of memory using calloc(). The code samples allocate memory, assign values, print the results, and free the allocated memory. Key points are that malloc() allocates memory for a single data type while calloc() allocates multiple blocks of the same type, and both return a pointer to the allocated memory that must later be freed.

Uploaded by

Ranjeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views3 pages

Dynamic Memory Allocation

The document discusses dynamic memory allocation in C using malloc() and calloc(). It provides sample code to demonstrate allocating memory for a single variable using malloc() and for multiple blocks of memory using calloc(). The code samples allocate memory, assign values, print the results, and free the allocated memory. Key points are that malloc() allocates memory for a single data type while calloc() allocates multiple blocks of the same type, and both return a pointer to the allocated memory that must later be freed.

Uploaded by

Ranjeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Dynamic Memory Allocations in C

1) Malloc in C :
malloc() is used to allocate memory space in bytes for variables of any valid C data type.

Syntax :

pointer= (data_type*)malloc(user_defined_size);

Sample Program :

malloc.c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,*ptr;
a=10;
ptr=(int*)malloc(a*sizeof(int));
ptr=a;
printf("%d",ptr);
free(ptr);
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built statement printf we


used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under conio.h
header files.
3. stdlib.h is used because malloc() comes under it.
4. int type variable a and pointer *ptr are declared.
5. Variable a is assigned a value of 10.
6. malloc() is used to allocate memory to pointer ptr.
7. The size given through malloc() to ptr is sizeof(int).
8. Now ptr is assigned the value of a. ptr=a; so the value 10 is assigned to ptr, for which,
we dynamically allocated memory space using malloc.
9. The value in ptr is displayed using printf
10. Then allocated memory is freed using the C in-built function free().

Output :

2) Calloc in C :
calloc() is sued to allocate multiple blocks of memory dynamically during the execution (run-
time) of the program.

Syntax :

pointer=(data_type*)calloc(no of memory blocks, size of each block in


bytes);

Sample Program :

calloc.c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *ptr,a[6]={1,2,3,4,5,6};
int i;
ptr=(int*)calloc(a[6]*sizeof(int),2);
for(i=0;i<7;i++)
{
printf("\n %d",*ptr+a[i]);
}

free(ptr);
getch();
}

Program Algorithm / Explanation :


1. #include<stdio.h> header file is included because, the C in-built statement printf we
used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under conio.h
header files.
3. stdlib.h is used because malloc() comes under it.
4. A Pointer *ptr of type int and array a[6] is declared.
5. Array a[6] is assigned with 6 values.
6. Another variable i of type int is declared.
7. Calloc() is used to allocate memory for ptr. 6 blocks of memory is allocated with each
block having 2 bytes of space.
8. Now variable i is used in for to cycle the loop 6 times on incremental mode.
9. On each cycle the data in allocated memory in ptr is printed using *ptr+a[i].
10. Then the memory space is freed using free(ptr).

Output :

You might also like