0% found this document useful (0 votes)
2 views

C 7

The document provides an overview of memory allocation, detailing static and dynamic memory allocation methods. It explains various memory allocation functions such as malloc, calloc, realloc, and free, along with example programs demonstrating their usage. The importance of proper memory management and the consequences of failing to release allocated memory are also highlighted.
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)
2 views

C 7

The document provides an overview of memory allocation, detailing static and dynamic memory allocation methods. It explains various memory allocation functions such as malloc, calloc, realloc, and free, along with example programs demonstrating their usage. The importance of proper memory management and the consequences of failing to release allocated memory are also highlighted.
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/ 13

SYNOPSIS

• Memory allocation
• Static Memory Allocation
• Memory Allocation Process
• Memory Allocation Functions
• Allocation A Block Of Memory : Malloc
• Allocation A Block Of Memory : Calloc
• Altering The Size Of A Block : Realloc
• Releasing The Used Space: Free
MEMORY ALLOCATION

• The blocks of information in a memory system is called memory


allocation.

• To allocate memory it is necessary to keep in information of


available memory in the system. If memory management system
finds sufficient free memory, it allocates only as much memory as
needed, keeping the rest available to satisfy future request.

• In memory allocation has two types. They are static and dynamic
memory allocation.
STATIC MEMORY ALLOCATION

• In static memory allocation, size of the memory may be required for


the that must be define before loading and executing the program.

DYNAMIC MEMORY ALLOCATION


• In the dynamic memory allocation, the memory is allocated to a
variable or program at the run time.

• The only way to access this dynamically allocated memory is


through pointer.
MEMORY ALLOCATION PROCESS
MEMORY ALLOCATION FUNCTIONS
ALLOCATION A BLOCK OF MEMORY :
MALLOC

malloc() function is used for allocating block of memory at


runtime. This function reserves a block of memory of given
size and returns a pointer of type void.

Ptr=(cast-type*) malloc (byte-size);


EXAMPLE PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct emp
{
int eno; Output:
char name; Enter the emp details
float esal; eno 1
void main() ename priya
{ esal 10,000
struct emp *ptr;
ptr = (struct emp *) malloc(size of(struct emp));
if(ptr == null)
{
pintf(“out of memory”);
}
else
{
printf(“Enter the emp deitals”);
scanf(“%d%s%f,&ptr-> eno,ptr-> name,&ptr-> esal”);
return 0;
}
ALLOCATION A BLOCK OF MEMORY :
CALLOC

calloc() is another memory allocation function that is used for


allocating memory at runtime. calloc function is normally used for
allocating memory to derived data types such as arrays and
structures.

Ptr=(cast-type*)calloc(n,elem-size);
EXAMPLE PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{ output:
int i, n;
int *a; Number of elements to be entered :3
printf("Number of elements to be entered:"); Enter 3 numbers:
scanf("%d",&n); 22
a = (int*)calloc(n, sizeof(int)); 55
printf("Enter %d numbers:\n",n); 14
for( i=0 ; i < n ; i++ ) The numbers entered are :22 55 14
{
scanf("%d",&a[i]);
}
printf("The numbers entered are: ");
for( i=0 ; i < n ; i++ )
{
printf("%d ",a[i]);
}
free( a );
return(0);
}
ALTERING THE SIZE OF A BLOCK : REALLOC

realloc() changes memory size that is already allocated


dynamically to a variable.

ptr=REALLOC(ptr,new size);
EXAMPLE PROGRAM
#include <stdio.h>
#include <stdlib.h>
int main()
{ Output:
int *ptr = (int *)malloc(sizeof(int)*2);
int i; 10 20 30
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++)
printf("%d ", *(ptr_new + i));
return 0;
}
RELEASING THE USED SPACE: FREE

Free() function should be called on a pointer that was used either with
”calloc()” or “malloc()”,otherwise the function will destroy the memory
management making a system to crash.

free (ptr)
EXAMPLE:
func()
{
int *ptr, *p;
ptr = new int[100];
p = new int;
delete[] ptr;
delete p;
}

You might also like