Explain Near Far Huge pointers in C language

CServer Side ProgrammingProgramming

Depending on Memory models and segment, pointers are classified into three types −

  • Near pointer
  • Far pointer
  • Huge pointer

Near pointer

  • It is a pointer which works with in the range of 64Kb data segment of memory.

  • It cannot access address beyond that data segment.

  • A near pointer can be incremented or decremented the address range by using an arithmetic operator.

  • With keyword near, we can make any pointer as near pointer.

Syntax

The syntax is as follows −

<data type> near <pointer definition>
<data type> near <function definition>

Following statement declares a near pointer for the variable s

char near *string;

Program

The following program shows the usage of near pointer.

#include<stdio.h>
int main(){
   int number=50;
   int near* p;
   p=&number;
   printf("%d",sizeof(p));
   return 0;
}

Output

The output is as follows −

2

Far pointer

  • It is a pointer that stores both offset and segment address to which the pointer is differencing.

  • It can access all 16 segments.

  • A far pointer address ranges from 0 to 1MB.

  • When the pointer is incremented or decremented, only the offset part is changing.

Syntax

The syntax is given below −

<data type> far <pointer definition>
<data type> far <function definition>

Following statements declares a far pointer for the variable s

char far *s;

Program

The following program shows the usage of far pointer.

#include<stdio.h>
int main(){
   int number=50;
   int far *p;
   p=&number;
   printf("%d",sizeof number);
   return 0;
}

Output

The output is as follows −

4

Huge Pointer

  • It is a pointer which is similar to far pointer in terms of size because, both are 32-bit address.

  • The huge pointer can be incremented without suffering with segment work round.

Program

The following program shows the usage of huge pointer.

#include<stdio.h>
Int main(){
   Char huge *far *ptr;
   Printf("%d%d%d",sizeof(ptr),sizeof(*ptr),sizeof(**ptr));
   Return 0;
}

Output

The output is as follows −

4 4 1
raja
Published on 10-Mar-2021 09:56:35
Advertisements