Open In App

Near, Far and Huge Pointers in C

In older times, the intel processors had 16-bit registers but the address bus was 20-bits wide. Due to this, the registers were not able to hold the entire address at once. As a solution, the memory was divided into segments of 64 kB size, and the near pointers, far pointers, and huge pointers were used in C to store the addresses.

There are old concepts used in 16-bit intel architectures, not of much use anymore.

Near, Far and Huge Pointers in C

1. Near Pointer

The Near Pointer is used to store the 16-bit addresses. It means that they can only reach the memory addresses within the current segment on a 16-bit machine. That is why we can only access the first 64 kb of data using near-pointers.

The size of the near pointer is 2 bytes.

Syntax of Near Pointers in C

pointer_type near * pointer_name;

Example of Near Pointers in C




// C Program to demonstrate the use of near pointer
#include <stdio.h>
 
int main()
{
    // declaring a near pointer
    int near* ptr;
 
    // size of the near pointer
    printf("Size of Near Pointer: %d bytes", sizeof(ptr));
    return 0;
}

Output

Size of Near Pointer: 2 bytes

Note: Most of the modern compiler will fail to run the above program as the concept of the near, far and huge pointers is not used anymore.

2. Far Pointer

A far pointer stores the address in two 16-bit registers that allow it to access the memory outside of the current segment. The compiler allocates a segment register to store the segment address, then another register to store offset within the current segment. The offset is then added to the shifted segment address to get the actual address.

Syntax of Far Pointer in C

pointer_type far * pointer_name;

Example of Far Pointer in C




// C Program to find the size of far pointer
#include <stdio.h>
 
int main()
{
    // declaring far pointer
    int far* ptr;
 
    // Size of far pointer
    printf("Size of Far Pointer: %d bytes", sizeof(ptr));
    return 0;
}

Output

Size of Far Pointer: 4 bytes

3. Huge Pointer

The huge pointer also stores the addresses in two separate registers similar to the far pointer. It has the following characteristics:

Syntax of Huge Pointer in C

pointer_type huge * pointer_name;

Example of Huge Pointer in C




// C Program to find the size of the huge pointer
#include <stdio.h>
 
int main()
{
    // declaring the huge pointer
    int huge* ptr;
 
    // size of huge pointer
    printf("Size of the Huge Pointer: %d bytes",
           sizeof(ptr));
    return 0;
}

Output

Size of the Huge Pointer: 4 bytes

Difference between Far Pointer and Near Pointer

Following are the difference between the far pointer and near pointer in C:

Difference between Far Pointer and Huge Pointer

Following are the difference between the far pointer and huge pointer in C:


Article Tags :