1. An Introduction to Dynamic Memory Allocation 2. Advantages of Dynamic memory allocation 3. Dynamic Memory Allocation in 'C'. 4. Allocating Memory 5. Releasing the allocated space 6. Allocating memory for derived data 7. Altering the allocated memory
DYNAMIC MEMORY ALLOCATION
1. An Introduction to Dynamic Memory Allocation
2. Advantages of Dynamic memory allocation
3. Dynamic Memory Allocation in 'C'.
4. Allocating Memory
5. Releasing the allocated space
6. Allocating memory for derived data
7. Altering the allocated memory
To
understand how dynamic memory allocation works, you must know how memory is
used, conceptually memory is divided into program memory and data memory.
Program
memory consists of memory used for main program and called functions. Data
memory consists of permanent definitions, such as global data and constants,
local definitions and dynamic data memory.

Obviously,
main must be in memory all the times, beyond main each called function must be
in memory only when it or any of its called functions are active. As a
practical matter most systems keeps all functions in memory while the program
is running.
Although,
the program code for a function may be in memory at all times, the local
variables for the functions are available only when it is active. In this case,
the multiple copies of the local variables are allocated, although only one
copy of the function is present. The memory facilities of these capabilities
are known as the stack. In addition to the stack, memory allocation known as
heap also available, this is unused memory allocated to the program and
variables to be assigned during its execution. It is the memory pool from which
memory allocated, when requested by the memory allocation function.
Dynamic
allocation is a pretty unique feature to C (amongst high level languages). It
enables us to create data types and structures of any size and length to suit
our programs need within the program.
The
data items in every 'C' program is dynamic in nature. i.e., the data items in
source program can be changed during the program execution.
So
far, we have used static memory. Static memory means which reserves a certain
amount of memory by default inside the program to use for variables and such.
There is nothing wrong with this, it means that once we reserve this memory, no
other program can use it, even if we are not using it at the time.
So,
if we have two programs that reserve 100 bytes of memory each, but neither
program is running, then we have 200 bytes of memory that is being have our two
programs that take 100 bytes each. Now we want to load a completely wasted.
Suppose, we only have 300 bytes of memory, but we already program that needs
150 bytes of memory. Well, we just hit a wall, because we only have 300 bytes
of memory and 200 bytes are already reserved. We can't load our third program
even though we have 200 bytes of memory that isn't even being used. How could
we possibly remedy this situation?
The
dynamic memory allocation is efficiently allowed to share the memory among all
three programs.
So,
let us imagine a new scenario. We have changed our first two programs to use
dynamic memory allocation. Now they only need to reserve 100 bytes of memory
each. This means we are now only using 200 bytes of our 300 total memory bytes
available. Our third program, which requires 150 bytes of memory can now run
fine.
Example: A program for processing a
list of students in a college. There are two possibilities can occur.
i)
The students names addition.
ii)
The students names deletion.
When
names are added the list grows, here we need to allocate more memory space to
the list for additional data. When names are deleted, the list shrinks, here we
need to reduce the allotted memory space.
Such
situation can be easily handled through the dynamic memory allocation, it is
very useful for easy and effective processing of data items in source program.
It
provides the flexibility in adding, deleting or rearranging data item at run
time.
•
It has the ability to reserve or allocate additional memory space during, the
program execution.
•
It has the ability to release unwanted memory space (i.e., previously
allocated) during the program execution.
•
It is very useful to modify the size of the previously allocated memory.
•
It is very useful to allocate memory space to an array of elements and
initialize them to zero.
Dynamic
memory allocation means, a program can obtain its memory while it is running.
It allows us to allocate additional memory space or to release unwanted space
at the time of program execution (runtime).
Pointers
support the dynamic memory allocation in 'C' language. The 'C' language provides
four library functions known as 'Memory
Management Functions' which can be used for allocating and releasing memory
during execution.
Dynamic
memory allocation function
malloc():Used
to allocate blocks of memory in required size of bytes.
free():
Used to release previously allocated memory space.
calloc():
Used to allocate memory space for an array of elements
realloc():
Used to modify the size of the allocated memory space.
Any
program, that uses these above functions must include the header file
<stdlib.h>
The
malloc() function is used to
allocate block of memory (i.e.) it allocates a block of memory of specified
size and return a pointer of type void.

After
the execution, the variable 'a' allocate 5 bytes of memory and it points to the
address of its first byte.

Where,
5 bytes of memory space is reserved and the address of first byte is stored in
pointer variable 'a'.
Example:
int *b;
b = (int *)malloc(10
*sizeof(int));
After
the execution, a memory space equivalent to 10 times size of an int, 20 bytes
is allocated and the address of the first byte of the memory is assigned to the
pointer variable 'b' type of 'int'.

Example: Program to
print variables from memory address.
/* Program to print variables from memory address */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
int *a, *n, size; /* *a,
*n is a pointer variables */
printf("Enter the size...");
scanf("%d", &size);
n = (int *)malloc(size * sizeof(int));
/* malloc() function allocate he memory size to the given
variables */
printf("Address of the first byte is.... %u\n", n);
printf("Enter the values...");
for(a=n; a <n+size; a++)
scanf("%d", a);
printf("Printing the values...\n");
for(a=n+size‒1;a> =n;a‒‒)
printf("%d is stored in address %u\n", *a, a);
} /* main */
OUTPUT
Enter the size...5
Address of the first
byte is....1952
Enter the values...1 2
3 4 5
Printing the values...
5 is stored in address
1960
4 is stored in address
1958.
3 is stored in address
1956
2 is stored in address
1954
1 is stored in address
1952
EXPLANATION:
The sizeof() function determines the size of data type required to store for
that data type. The malloc() function determine the memory to allocate that size
of the variable.
The
free() function is used to release the previously allocated memory space using
malloc() or calloc() i.e., it is the opposite of malloc() function.
Syntax:
void free(void *p);
or
free(p);
Description:
'p' is the pointer to a memory, which has
already been allocated by using malloc() or calloc()

The
calloc() function is used for
allocating memory space during the program execution for derived data types
such as arrays, structures, etc.
The
malloc() allocates a single block of
memory where as calloc() allocates
multiple block of memory with same size and initializes them with zeros.
Syntax:
pointer variable =
(type_cast *)calloc(n, element size)
Description:
n: number of blocks
element size size: in
bytes for each elements
All
the bytes are initialised with zero and the address of the first byte is
pointer to the pointer variable.
Example:
struct book
{
int no;
char name[10];
float cost;
};
struct book bl;
b1 *sptr;
sptr=(book) calloc(10, sizeof(book));
After
the execution of this code, it allocates the storage space for the structure
book with specified members. The calloc() function allocates memory to hold 10
records.
It
is necessary to alter the previously allocated memory. i.e., to add additional
memory or to reduce as and when required.
For
the above purposes, the realloc() function is very useful and this process is
called reallocation of memory.
Before
using this statement, the user must allocate some memory previously by using
the malloc() function.
Syntax:
pointer
variable=malloc(size);
After,
the reallocation of memory can be done by using the realloc()

Example: Program to
altering the allocated memory.
* Program to altering the allocated memory */
#include <stdio.h>
#include <stdlib.h>
main()
{
char *p; /**p is a pointer variables */
p=(char *)malloc(6); /* malloc() allocate memory to variables */
strcpy(p, "MADRAS"); /* string copy */
printf("Memory contains: %s\n",p)
p=(char *)realloc(p, 7)
/reallocation */
strcpy (p, "CHENNAI"); /* string copy */
printf("Memory now contains: %s\n", p);
free (p); /* Releasing memory*/
free(p);
} /* main */
OUTPUT
Memory contains:
MADRAS
Memory now contains:
CHENNAI
EXPLANATION:
The function malloc() function 6 bytes are allocated to character pointer p.
The character pointer is initialised with string MADRAS. To store more than 6
characters, we need to allocate more bytes to pointer 'p'. Using realloc()
function memory, allocation takes place. After reallocation the pointer
contains 7 bytes. The pointer p is again initialised with CHENNAI. The output
display contents of p before and after reallocation. The free() function
release the memory allocation.
Computer Programming C: UNIT IV: Structures and Unions : Tag: Computer Science : C Programming - Dynamic Memory Allocation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation