The Wayback Machine - https://web.archive.org/web/20240717024544/https://www.geeksforgeeks.org/what-are-near-far-and-huge-pointers/
Open In App

Near, Far and Huge Pointers in C

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 pointer far pointer huge pointer in Cr in

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




// 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.

  • In the far pointer, the segment part cannot be modified as incrementing/decrementing only changes the offset but not the segment address.
  • The size of the far pointer is 4 bytes.
  • The problem with the far pointers is that the pointer has different values but points to the same address. So, the pointer comparison is useless on the far pointers.

Syntax of Far Pointer in C

pointer_type far * pointer_name;

Example of Far Pointer in C

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:

  • In the Huge pointer, both offset and segment address is changed.  That is why we can jump from one segment to other using a Huge Pointer.
  • The Huge Pointers always compare the absolute addresses, so the relational operation can be performed on it.
  • The size of the huge pointer is 4 bytes.

Syntax of Huge Pointer in C

pointer_type huge * pointer_name;

Example of Huge Pointer in C

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:

  • The far pointer can store the address of any memory location in the RAM whereas the near pointer can only store till first 64kB addresses.
  • The far pointer uses two registers to store segment and offset addresses separately whereas the near pointer can only store addresses in a single register.
  • The size of the far pointer is 4 bytes where as the near pointer is 2 bytes wide.

Difference between Far Pointer and Huge Pointer

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

  • The far pointer cannot move between different segments whereas the huge pointer can move between multiple memory segments.
  • The two far pointer values can point to the same location while in the case of the huge pointer, it is not possible.


Previous Article
Next Article

Similar Reads

Implement your own tail (Read last n lines of a huge file)
Given a huge file having dynamic data, write a program to read last n lines from the file at any point without reading the entire file. The problem is similar to tail command in linux which displays the last few lines of a file. It is mostly used for viewing log file updates as these updates are appended to the log files. Source : Microsoft Intervi
4 min read
Difference between pointer to an array and array of pointers
Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the
4 min read
Features and Use of Pointers in C/C++
Pointers store the address of variables or a memory location. Syntax: datatype *var_name; Example: pointer "ptr" holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through "ptr" int *ptr; Features of Pointers: Pointers save memory space.Execution time with pointers is faster beca
2 min read
How to Declare and Initialize an Array of Pointers to a Structure in C?
Prerequisite: Structure in CArray in C In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers in C language. Today we will learn how to create
8 min read
Difference between Arrays and Pointers
The array and pointers are derived data types that have lots of differences and similarities. In some cases, we can even use pointers in place of an array, and arrays automatically get converted to pointers when passed to a function. So, it is necessary to know about the differences between arrays and pointers to properly utilize them in our progra
7 min read
Dangling, Void , Null and Wild Pointers in C
In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deallocated memory), Void (pointing to some data locati
6 min read
Program to reverse an array using pointers
Prerequisite : Pointers in C/C++ Given an array, write a program to reverse it using pointers . In this program we make use of * operator . The * (asterisk) operator denotes the value of variable . The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the memory location pointed by the pointer .
4 min read
Program to Reverse a String using Pointers
Given a string, the task is to reverse this String using pointers. Examples: Input: Geeks Output: skeeG Input: GeeksForGeeks Output: skeeGroFskeeG Approach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then reversed one by one with the help of these t
2 min read
Move all zeroes to end of array using Two-Pointers
Given an array of random numbers, Push all the zero’s of the given array to the end of the array. For example, if the given arrays is {1, 0, 2, 6, 0, 4}, it should be changed to {1, 2, 6, 4, 0, 0}. The order of all other elements should be the same.Examples: Input: arr[]={8, 9, 0, 1, 2, 0, 3} Output: arr[]={8, 9, 1, 2, 3, 0, 0} Explanation: Swap {0
6 min read
How many levels of pointers can we have in C/C++
Prerequisite: Pointer in C and C++, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable and the value of a variable can be accessed using dereferencing of the pointer. A pointer is generally initialized as: datatype *variable name; This above declaration i
7 min read
Article Tags :
three90RightbarBannerImg