Presentation 35951 Content Document 20250506023643PM
Presentation 35951 Content Document 20250506023643PM
By Dr.Shashikiran V
Memory Allocation in C Programs
• Global Variables in C programs
• These are visible to all the functions in a C program. The required memory space for these variables is
allocated by the compiler/Operating System before the program execution begins.
• The space allocated for these variables are released only when the program ends.
• This type of allocation is called static memory allocation.
• Local variables in C program
• These variables are visible only within the function (or basic block) within which they are declared and
any nested blocks inside them.
• Space for local variables is allocated just when the function is invoked in a region of memory called the
stack. Memory is freed when the function returns.
• Size allocated is based on the number of local variables declared.
• Modern C language allows variable length arrays where the length of the array can be determined at
run-time.
• There is no way to free the allocated memory till the end of the function.
• Both these types of memory allocation are called static memory allocation.
Dynamic Memory allocation – Why do we
need it?
• There are many real world situations where it will be convenient to allocate
the amount of memory needed based on user input and also be able to
release the same after the program has finished some set of operations.
• In static allocation, memory is allocated to the program by the Operating
system and is locked and not available for use by other programs running on
the system.
• It is not always possible to determine the amount of memory needed apriori
and allocate appropriately.
• If we allocate a lesser amount, it becomes cumbersome to handle more data than the
allocated amount.
• If we allocate a much larger amount, we are wasting memory that could have been
otherwise used by other programs.
Dynamic Memory - Basics
• There is a chunk (a certain amount) of memory that is managed by a library
component that provides calls to allocate and free the desired size blocks from
the same.
• This chunk of memory is generally called as ‘Heap Memory’.
• The library makes the required calls to the Operating System to allocate and free
the heap memory as needed (All the memory in a computer is managed by the
Operating System).
• Once it gets a large chunk of memory from the Operating system it may use
optimal algorithms to allocate and free smaller portions from it as the program
requests using the malloc() and free() calls.
• Most real world programs make use of dynamic memory calls to allocate and free
memory as needed.
Basic calls for Dynamic memory management
• void *malloc(size_t size)
• This is the basic call to allocate a memory block of ‘size’ bytes.
• The call returns a ‘void *’ pointer which can be cast to the appropriate data type
and utilized
• Need to always check if the malloc() call is successful in allocating the size required.
• If malloc() cannot allocate the required memory, it will return ‘NULL’ which can be used to
understand the failure and handle it appropriately.
• void free(void *ptr)
• free() frees the chunk of memory that is pointed to by ‘ptr’. It is important to make
sure that ‘ptr’ is a value returned by a previous call to ‘malloc()’.
• Behaviour is undefined and can cause errors if ‘ptr’ is not a value that was returned
earlier by ‘malloc()’.
Additional calls
• void *calloc(size_t nmemb, size_t size)
• This allocates space for ‘nmemb’ items of size ‘size’.
• Additionally, this call also ensures that the allocated memory is initialized to the value 0
(binary 0x0).
• Memory allocated by ‘calloc()’ is also freed using ‘free()’
• void *realloc(void *ptr, size_t size)
• This is used to either expand or reduce the amount of memory allocated by a previous
call to malloc() or calloc().
• ‘ptr’ should be a value allocated by one of the previous allocation calls.
• ‘size’ is the new size that is desired.
• The returned pointer points to a chunk of memory which is of the new size. If the
memory contents have to be moved, the returned pointer may be different than the
‘ptr’ value passed as the first parameter.
Using Dynamic memory
#include <stdio.h>
#include <stdlib.h> /* We need to include this header for using the
malloc(), free() calls */
{
char *cp; /* pointer to a character (array) */
cp = (char *) malloc( (size_t) (100 * sizeof(char)); /* allocate space for 100
chars */
if(cp == NULL) {
printf(“malloc() failed\n”);
exit(-1);
}
*cp = ‘H’;
*(cp+1) = ‘E’;
*(cp+2) = ‘\0’; /* null termination */
printf(“%s”,cp);
free(cp);
}
Example 01
• ##include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n, i;
return 0;
}
Example 02
Write a program that uses a table of integers whose size will be specified interactively at run time.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int *p, *table;
int size;
clrscr();
printf("\nWhat is the size of the table");
scanf("%d",&size);
table = (int *)malloc(size * size(int)); // allocates memory for specified numbers using malloc
printf("Enter %d numbers", size);
for(p=table; p<table + size; p++)
scanf("%d",p);
for(p=table; p<table + size; p++)
printf("%d is stored at address %u",*p, p);
getch();
}
//Same program using calloc function
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int *p, *table;
int size;
clrscr();
printf("\nWhat is the size of the table");
scanf("%d",&size);
table = (int *)calloc(size, size(int)); // allocates memory for specified numbers using calloc
printf("Enter %d numbers", size);
for(p=table; p<table + size; p++)
scanf("%d",p);
for(p=table; p<table + size; p++)
printf("%d is stored at address %u",*p, p);
getch();
}.