Nothing Special   »   [go: up one dir, main page]

ADP - Pointer and Dynamic Memory Allocation

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

C Pointers

Struck off

The pointer in C language is a variable which stores the address of another variable. This
variable can be of type int, char, array, function, or any other pointer. The size of the
pointer depends on the architecture. However, in 32-bit architecture the size of a
pointer is 2 byte.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol).
It is also known as indirection pointer used to dereference a pointer.
1. int *a; //pointer to int
2. char *c; //pointer to char
Pointer Example

As you can see in the above figure, pointer variable stores the address of number
variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable
p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable p.
Let's see the pointer example as explained for the above figure.
#include<stdio.h>
int main ( ) {
int number=50;
int *p;
p=&number;
printf("Address of p variable is %x \n", p);
printf("Value of p variable is %d \n", *p);
return 0;
}
Outputs of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions
where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the
code and improves the performance.
NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer. If you
don't have any address to be specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.
int *p=NULL;
In the most libraries, the value of the pointer is 0 (zero).
Difference between static memory allocation and dynamic memory
allocation
static memory allocation dynamic memory allocation

memory is allocated at compile time. memory is allocated at run time.

memory can't be increased while executing memory can be increased while executing
program. program.

used in array. used in linked list.

Methods used for dynamic memory allocation

malloc( ) allocates single block of requested memory.

calloc( ) allocates multiple blocks of requested memory.

realloc( ) reallocates the memory occupied by malloc( ) or calloc( ) functions.

free( ) frees the dynamically allocated memory.

malloc( ) function in C
The malloc( ) function allocates single block of requested memory.
It doesn't initialize memory at execution time, so it has garbage value initially.
The syntax of malloc( ) function is given below:
ptr= (cast-type*) malloc ( byte-size )
Let's see the example of malloc() function.
#include<stdio.h>
#include<stdlib.h>
int main ( ){
int n, i, *ptr, sum=0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr=( int* ) malloc (n * sizeof (int) ); //memory allocated using malloc
printf("Enter elements of array: ");
for(i=0; i<n; i++) {
scanf("%d", ptr+i);
sum=sum + *(ptr+i);
}
printf("Sum=%d", sum);
free ( ptr );
return 0;
}
Output
Enter elements of array: 3
Enter elements of array: 10
10
10
Sum=30
calloc() function in C
The calloc() function allocates multiple block of requested memory.
It initially initializes all bytes to zero.
The syntax of calloc() function is given below:
1. ptr=(cast-type*)calloc(number, byte-size)
Let's see the example of calloc() function.
#include<stdio.h>
#include<stdlib.h>
int main( ){
int n, i , *ptr, sum=0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr=(int*) calloc (n, sizeof(int)); //memory allocated using calloc

printf("Enter elements of array: ");


for(i=0; i<n; i++)
{
scanf("%d", ptr+i);
sum=sum + *(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Output
Enter elements of array: 3
Enter elements of array: 10
10
10
Sum=30
realloc ( ) function in C
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by
realloc() function. In short, it changes the memory size.
Let's see the syntax of realloc() function.
ptr=realloc(ptr, new-size)
free() function in C
The memory occupied by malloc ( ) or calloc ( ) functions must be released by calling
free() function. Otherwise, it will consume memory until program exit.
Let's see the syntax of free() function.
free(ptr)

You might also like