[go: up one dir, main page]

0% found this document useful (0 votes)
27 views19 pages

DMA in C

The document provides an overview of Dynamic Memory Allocation (DMA) in C, detailing four key functions: malloc(), calloc(), free(), and realloc(). It explains how each function works, including their syntax and usage, along with examples. Additionally, it covers the concept of pointers in C, specifically void pointers and constant pointers.

Uploaded by

r.u.easwars
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)
27 views19 pages

DMA in C

The document provides an overview of Dynamic Memory Allocation (DMA) in C, detailing four key functions: malloc(), calloc(), free(), and realloc(). It explains how each function works, including their syntax and usage, along with examples. Additionally, it covers the concept of pointers in C, specifically void pointers and constant pointers.

Uploaded by

r.u.easwars
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/ 19

DMA in C

Dynamic Memory Allocation in C


Dynamic Memory Allocation can be defined as a procedure in which
the size of a data structure (like Array) is changed during the runtime.

There are 4 library functions provided by C defined


under <stdlib.h> header file to facilitate dynamic memory allocation in
C programming. They are:

1.malloc()
2.calloc()
3.free()
4.realloc()
malloc() method

The “malloc” or “memory allocation” method in C is used to dynamically


allocate a single large block of memory with the specified size.

It returns a pointer of type void which can be cast into a pointer of any form.
It doesn’t Initialize memory at execution time so that it has initialized each
block with the default garbage value initially.
Syntax of malloc() in C

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

For Example:

ptr = (int*) malloc(100 * sizeof(int));

Since the size of int is 4 bytes, this statement will allocate


400 bytes of memory. And, the pointer ptr holds the
address of the first byte in the allocated memory.
If space is insufficient, allocation fails and returns a NULL
pointer.
Output:

Enter number of elements:7


Entered number of elements: 7
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7,
calloc() method

“calloc” or “contiguous allocation” method in C is used to


dynamically allocate the specified number of blocks of memory of
the specified type. it is very much similar to malloc() but has two
different points and these are:

2.It initializes each block with a default value ‘0’.


3.It has two parameters or arguments as compare to malloc().
Syntax of calloc() in C
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each
element.

For Example:

ptr = (float*) calloc(25, sizeof(float));


This statement allocates contiguous space in memory for 25
elements each with the size of the float.
If space is insufficient, allocation fails and returns a NULL pointer.
C free() method

“free” method in C is used to dynamically de-allocate the memory.


The memory allocated using functions malloc() and calloc() is not de-
allocated on their own. Hence the free() method is used, whenever the
dynamic memory allocation takes place.

It helps to reduce wastage of memory by freeing it.

Syntax of free() in C

free(ptr);
C realloc() method

“realloc” or “re-allocation” method in C is used to dynamically change the


memory allocation of a previously allocated memory. In other words, if the
memory previously allocated with the help of malloc or calloc is insufficient,
realloc can be used to dynamically re-allocate memory. re-allocation of
memory maintains the already present value and new blocks will be
initialized with the default garbage value.
Syntax of realloc() in C

ptr = realloc(ptr, newSize);


where ptr is reallocated with new size 'newSize'.
If space is insufficient, allocation fails and returns a NULL pointer
Types of Pointers
void Pointer in C

A void pointer is a pointer that has no associated data type with it.

A void pointer can hold an address of any type and can be typecasted to
any type.
// C Program to demonstrate that a void pointer
// can hold the address of any type-castable type
#include <stdio.h>
int main()
{
int a = 10;
char b = 'x';
// void pointer holds address of int 'a'
void* p = &a;
// void pointer holds address of char 'b'
p = &b;
}
#include <stdio.h>
int main()
{
int a = 10; Output:
char b = 'x'; 10
// void pointer holds address of int 'a' x
void* p = &a;
printf("%d\n",*(int *)p);
// void pointer holds address of char ‘b'
p=&b;
printf("%c", *(char *)p);
}
constant pointer in C

A constant pointer in C cannot change the address of the variable to


which it is pointing, i.e., the address will remain constant.

Therefore, we can say that if a constant pointer is pointing to some


variable, then it cannot point to any other variable.
#include <stdio.h>
int main()
{ main.c: In function ‘main’: main.c:7:9:
int a=1; error: assignment of read-only variable
int b=2; ‘ptr’
int *const ptr=&a;
7 | ptr=&b; | ^
ptr=&b;
printf("Value of ptr is :%d",*ptr);
return 0;
}

You might also like