Memory Management: Chapter 3 - Principles of Data Structures Using C by Vinu V Das
Memory Management: Chapter 3 - Principles of Data Structures Using C by Vinu V Das
Management
Chapter 3 - Principles of Data Structures using C by Vinu V Das
Contents
1D and 2D
What is Arrays Single and
Memory DMA Multiple Block
Allocation.
Static Reallocation
Memory in C.
Allocation
Dynamic
Memory De-allocation
Allocation Recap of
in C.
C&
Pointers
Example :
When this statement is compiled, two bytes for each variable „i‟ and „j‟ will be allocated.
This statement will allocate 20 bytes to the array A (5 elements of floating point type, i.e., 5 × 4) and
four bytes for the variable „f ‟.
Write :
• If you try to assign values to 15 elements of an array whose size is declared as 10, then first 10
elements can be assigned and the other 5 elements cannot be assigned/accessed.
Inefficient Utilization :
• Another problem with static memory allocation is that if you store less number of elements than
the number of elements for which you have declared memory, and then the rest of the memory
will be wasted. That is the unused memory cells are not made available to other applications (or
process which is running parallel to the program) and its status is set as allocated and not free. This
leads to the inefficient use of memory.
C language provides following dynamic allocation and de-allocation functions in <stdlib.h> library :
Allocation :
• malloc( )
• calloc( )
• realloc( )
De-allocation :
• free( )
Example :
• int m = 25;
• int * itemp; // a pointer to an integer or integer type pointer
The above statements allocate storage for an int variable m and a pointer variable itemp.
It applies the unary address of operator & to m to get its address, which is then stored in itemp .
This is the same & operator that appears in scanf( ) functions in C language.
p1 = &firstvalue;
p2 = &secondvalue;
p3 = &thirdvalue;
*p1 = 10;
*p2 = *p3;
p1 = p3;
*p1 = *p2;
p2 = p3;
*p2 = 50;
p3 = &firstvalue;
*p3 = *p1;
„ptr‟ is a pointer of any type, „any_type‟ byte size is the allocated area of memory block.
Example :
• ptr = (int *) malloc (10 * sizeof (int));
On execution of this statement, 10 times memory space equivalent to size of an „int‟ byte is allocated
and the address of the first byte is assigned to the pointer variable „ptr‟ of type „int‟.
• Remember the malloc( ) function allocates a block of contiguous bytes. The allocation can fail if the
space in the heap is not sufficient to satisfy the request. If it fails, it returns a NULL pointer. So it is
always better to check whether the memory allocation is successful or not before using it.
• The memory allocated using malloc( ) function contains garbage values by default.
Instructor: Aatif Shahbaz 19
Malloc( ) - Example
A program to find the sum of N elements using dynamic memory allocation
#include<stdio.h>
#include<conio.h>
void main(void)
{ int i, j, n, sum =0, *ptr;
#include<stdio.h>
#include<conio.h>
void main(void)
{ int i, j, n, sum =0;
getch( );
}
Hypothesis :
The program generate a compilation error that “an array subscript must be a constant value”.
Array is a static data structure according to ISO C, C++ standards.
• Its memory allocation must be done at compile time.
• Run time allocation is not compatible with arrays.
Solution:
Use malloc( ) if dynamic memory allocation is required.
The calloc( ) function works exactly similar to malloc( ) function except for the fact that it needs two
arguments as against one argument required by malloc( ) function.
The above statements allocates contiguous space for „n‟ blocks, each of size of block_size bytes.
Example:
• ptr = (int *) calloc(25,sizeof (float));
Here, the size of data type in byte for which allocation is to be made (4 bytes for a floating point
numbers) is specified and 25 specifies the number of elements for which allocation is to be made.
Instructor: Aatif Shahbaz 24
Calloc( ) - Example
A program to find the sum of N elements using dynamic memory allocation
#include<stdio.h>
#include<conio.h>
void main(void)
{ int i, j, n, sum =0, *ptr;
ptr=(int*)calloc(n, sizeof(int)); // Allocating memory space for n integers of int type to *ptr
Where „ptr‟ is a pointer holding the starting address of the allocated memory block. And New_Size is
the size in bytes that the system is going to reallocate.
„ptr‟ is a pointer to a memory block which has already been allocated by malloc() or calloc() functions.
• Trying to release an invalid pointer may create problems and cause system crash.
The dynamically allocated memory cells are not made available to other applications and its status
remains set to allocated, till free( ) function has been called, to release it.
#include<stdio.h>
#include<conio.h>
void main(void)
{ int i, j, n, sum =0, *ptr; //Allocate memory space for two-integer pointer variable
x = (int**)calloc(dimension1 , sizeof(int*)); x
for (int i = 0; i < dimension1 ; i++)
{
[0]
x[ i ] = (int*)calloc(dimension2 , sizeof(int));
} [1]
[2]
De-allocation :
[3]
for (int i = 0; i < dimension1 ; i++)
{ [4]
free(x[ i ]); [5]
}
free(x); Also called DYNAMIC ARRAY
Instructor: Aatif Shahbaz 32
Dynamic Array
realloc( ) -
free( ) delete