Near Far And Huge Pointers In C Language

  • Far pointer
  • Huge pointer
  • Near pointer

In a general context, the near point serves the purpose of holding addresses, limited to a maximum size of 16 bits. By utilizing the near pointer, it is feasible to store addresses that do not exceed the 16-bit constraint. While this limitation may seem restrictive, enabling storage of smaller addresses within the 16-bit range is attainable. The downside of the near pointer becomes evident as it restricts access to only 64kb of data at a time, highlighting a significant drawback and contributing to its declining popularity. Exploring further essential aspects can enhance our comprehension of the near pointer.

Syntax

The syntax is as follows -

Example

<data type> near <pointer definition>

The line below initializes a near pointer for the variable s.

Example

char near *string;

Lets us see some important key points that can help us to understand the " Near " pointer.

  • Near pointer cannot access beyond the data segment like graphics video memory, text video memory, etc. The size of the near pointer is two bytes. With the help of keyword near, we can make any pointer a near pointer.
  • It is a pointer that works within the range of the 64Kb data segment of memory.
  • It cannot access addresses beyond the given data segment.
  • A near pointer can be incremented or decremented in the address range by using an arithmetic operator.

To delve deeper into the concept of the "Near" pointer, let's examine an illustrative example:

Example

Example

#include<stdio.h>

    int main()
      {
          int x=25;
          int near* ptr;
          ptr=&x;
          printf("%d",sizeof ptr);
          return 0;
      }

Output

Far Pointer

In most cases, a Far pointer is commonly recognized as a 32-bit pointer. Nevertheless, it has the capability to retrieve data located beyond the computer's memory from the present segment. To employ this pointer variant, it's customary to reserve the sector register for holding the data address within the current segment. Furthermore, an additional register allocation is necessary to hold the offset value inside the current segment.

Syntax

The syntax is given below:

Example

<data type> far <pointer definition>

The subsequent lines declare a far pointer for the variable ```

<data type> near <pointer definition>

Example


char far *s;

Example


Lets us see some other important key points that can help us to understand the Far pointer.

- When the pointer is incremented or decremented, only the offset part is changed.

- It is such a type of pointer that stores both offset and segment address to which the pointer is differencing.

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

- It can access all 16 segments.

Example

include<stdio.h>

int main{

int number=50;

int far *p;

p=&number;

printf("%d",sizeof number);

return 0;

}

Example


Output

### Huge pointers

Similar to the far pointer, the huge pointer is also 32 bits in size, enabling access to data beyond the segment boundaries. While the far pointer shares similarities with the huge pointer, it offers distinct advantages. For instance, the far pointer is static, preventing any alterations to the segment it points to. In contrast, the huge pointer is dynamic, providing flexibility for modifications within the segment it references.

Let's explore additional essential aspects that can enhance our comprehension of the "Huge" pointer.

- Referring to any memory segment, a huge pointer is capable of this task. It possesses a dimension of 4 bytes or 32-bits, enabling it to reach a memory size of up to 64K.

- Incrementing a huge pointer does not involve encountering segment workarounds.

The provided example illustrates how to utilize the large pointer.

Example:

include<stdio.h>

int main

{

char huge far p;

printf("%d %d %d",sizeof(p),sizeof(p),sizeof(*p));

return 0;

}

Example


Output

In this provided code, p represents a large pointer, *p signifies a distant pointer, and **p denotes a character-type data variable.

### What is the difference between near, far, and huge pointers?

Generally, a virtual address consists of a selector and an offset. Unlike a near-pointer that lacks a distinct selector, huge pointers include a specific selector. When conducting pointer calculations with a far pointer, the selector remains unchanged; however, in the scenario of a huge pointer, it may be altered.

#### Note: These are non-standard keywords, also implementation-specific, and are irrelevant in modern platforms.

Input Required

This code uses input(). Please provide values below: