Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views22 pages

Dynamic Memory Allocation

Uploaded by

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

Dynamic Memory Allocation

Uploaded by

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

Dynamic Memory Allocation

Dynamic Memory Allocation


• The process of allocating memory during run
time(Execution time) is known dynamic
memory allocation.
• Ability of a program to use more memory
space at execution time
• Memory space required can be specified at
the time of execution.
• C supports allocating and freeing memory
dynamically using library routines.
• These functions are available in stdlib.h
Memory Allocation Functions
• malloc – Allocates requested number of bytes
and returns a pointer to the first byte of the
allocated space.
• calloc – Allocates space for an array of
elements, initializes them to zero and then
returns a pointer to the memory.
• Free- Frees previously allocated space.
• realloc – Modifies the size of previously
allocated space.
malloc()
• The name "malloc" stands for memory
allocation.
• The malloc() function reserves a block of
memory of the specified number of bytes.
• It returns a pointer of void which can be
casted into pointers of any form.
Syntax of malloc()
• voidptr = (castType*) malloc(size);
Examples
• int *p;
• p = (int *) malloc (100 * sizeof (int)) ;
• A memory space equivalent to “100 times the
size of an int” bytes is reserved.
• The address of the first byte of the allocated
memory is assigned to the pointer p of type
int.

P
Example
• float *ptr;
• ptr = (float*) malloc(100 * sizeof(float));
• The above statement allocates 800 bytes of
memory. It's because the size of float is 8
bytes.
• And, the pointer ptr holds the address of the
first byte in the allocated memory.
Write statements
• Allocate memory of storing 50 integers
• Allocate memory of storing 40 characters
• Allocate memory of storing 120 double values
• Allocate memory of storing 150 floating point
values
Answers
• Allocate memory of storing 50 integers
iVar=(int*)malloc(50*sizeof(int));

• Allocate memory of storing 40 characters


cVar=(char*)malloc(40*sizeof(char));

• Allocate memory of storing 120 double values


dVar=(double*)malloc(120*sizeof(double));

• Allocate memory of storing ‘n’ floating point values


fVar=(float*)malloc(n*sizeof(float));
Example
int*ip;
ip= (int*) malloc(10 * sizeof(int));
If (ip== NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
C program to input and print text
using Dynamic Memory Allocation
#include <stdio.h> printf("Inputted text is: %s\n",text);
#include <stdlib.h> /*Free Memory*/
int main() free(text);
{ return 0;
int n; }
char *text; Output:
printf("Enter limit of the text: "); Enter limit of the text: 100
scanf("%d",&n); Enter text: Welcome to C
/*allocate memory dynamically*/ Programming.
text=(char*)malloc(n*sizeof(char)); Inputted text is: Welcome to C
printf("Enter text: "); Programming.
scanf(“%d”,text);
calloc()
• The name "calloc" stands for contiguous allocation.
• calloc() allocates multiple blocks of memory each of
same size and sets all bytes to zero.
• calloc() takes two arguments:
– the number of blocks to be allocated
– the size of each block (in bytes)
• calloc() returns the address of the chunk of memory
that was allocated
• calloc() also sets all the values in the allocated memory
to zeros
• calloc()is also used to dynamically allocate arrays
Syntax and Example
Syntax:
ptr = (castType*)calloc(n, size);
Example 1:
• ptr = (float*) calloc(25, sizeof(float));
• The above statement allocates contiguous space in memory
for 25 elements of type float.
Example2:
For instance, to dynamically allocate an array of 10 ints:
• int*arr;
• arr= (int*) calloc(10, sizeof(int));
• /* now arrhas the address
• of an array of 10 ints, all 0s */
Difference between malloc() and
calloc()
• malloc() allocates single block of memory
whereas leaves the memory uninitialized.
• calloc() function allocates multiple blocks of
memory of same size and initializes all bits to
zero.
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.
Syntax of free()
free(ptr);
• This statement frees the space allocated in the
memory pointed by ptr.
Program to calculate the sum of n numbers
entered by the user using calloc()
#include <stdio.h> exit(0); }
#include <stdlib.h> printf("Enter elements: ");
int main() for(i = 0; i < n; ++i)
{ {
int n, i, *ptr, sum = 0; scanf("%d", ptr + i);
printf("Enter number of elements: sum += *(ptr + i);
"); }
scanf("%d", &n); printf("Sum = %d", sum);
ptr = (int*) calloc(n, sizeof(int)); free(ptr);
if(ptr == NULL) return 0;
{ }
printf("Error! memory not
OUTPUT: Enter number of elements: -1
allocated.");
Replace this code in the highlighted one in the
previous slide
ptr = (int*) calloc(-1, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0); }

ptr = (int*) calloc(n, sizeof(int));


if(ptr == NULL)
{
printf("Error! memory not allocated.");
}
Realloc
• It is used to dynamically change the memory
allocation of a previously allocated memory.
• If the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used
to dynamically re-allocate memory.
Syntax:
• ptr= realloc(ptr, num_bytes);
• where ptr is reallocated with new size 'newSize'.
–Find space for new allocation
–Copy original data into new space
–Free old space
–Return pointer to new space

You might also like