Define Pointer? Explain All Function Of DMA (Dynamic Memory Allocation).

3 years ago
C Programming

Pointer: – Pointer means that stores another variable address. The pointers are same to the same variable but the difference between the variable and the pointer. The variable stored any valuable items. And pointer stored another variable address.

Declaration Of Pointer

 The Declare of the pointer using Asteric (*) Sign. This asteric sign is also known as Indirection Operator.

Syntax

int *ptr;

Note – The given ptr is the pointer name.

Initialization Of Pointer 

Initialization pointer means to assign the address of another variable.

Ex –

int * ptr;

ptr = &age;

Stored the address in any variable into a pointer with using the address operator (&).

Function Of DMA (Dynamic Memory Allocation)

 “The Dynamic Memory Allocation (DMA) is used to define the array size at a run time.” The compiler does not know the size of an array. The DMA all operation performs the given function. The given function is a library function.

Dynamic Memory Allocation Function 

There is some function of dynamic memory allocation. It is given below 1, malloc()

2, calloc()

3, free()

4, realloc()

1, malloc() :- malloc stand for memory allocation. It takes several bytes to be as an input and returns a pointer of type void.

Syntax – 

The given 30 are entered by the user. ptr = (int*)malloc(30*sizeof(int));

The Expression returns a null pointer if the memory is not allocated.

2, calloc() :- calloc() stands for continuous allocation. It initializes each memory block with a default value of 0.

Syntax –

The given 30 are entered by the user. ptr = (float*)calloc(30,sizeof(float));

if the space is not sufficient, memory allocation fails a null pointer is returned.

3, free():- we can use this function to be deallocated the memory. The memory allocation using malloc/calloc is not deallocated automatically.

Syntax –

free(ptr);

The memory of ptr is released.

4, realloc():- Sometimes the dynamically allocated memory is insufficient or more than required. Realloc is used to allocate memory of new size using the previous pointer and new size.

Syntax – 

ptr = realloc(ptr, new size);

ptr = realloc(ptr,3*sizeof int);

2
Rusma Khadka
Dec 27, 2022
More related questions

Questions Bank

View all Questions