Update appNew update is available. Click here to update.

Near, Far and Huge Pointers

Aditi
Last Updated: Jun 20, 2022
EASY

Introduction

In this blog, we will study several ancient principles that were often utilized and implemented in 16-bit Intel architectures in the early days of Ms-Dos but are no longer widely used. Nonetheless, these notions are critical since they serve as the foundation for present technologies. Pointers, on the other hand, can be divided into three categories based on the Memory model and segment:

  • Near pointer
  • Far pointer
  • Huge pointer

These are some outdated notions from the days of MS-DOS employed in 16-bit Intel architectures.

Near Pointer

On a 16-bit system, a near-pointer is used to hold 16-bit addresses within the current segment. We can only access 64kb of data at a time due to the constraint.

A near pointer is a pointer that is used to store bit addresses of up to 16 bits in a 16-bit enabled portion of computer memory. The primary problem is that it can only access data of limited size (approximately 64 kb) in a particular period.

Syntax:

<data type> near <pointer definition>

 

For example, 

char near *string; 

 

Some important points about Near Pointer are as follows:-

  • The near pointer is unable to access data segments such as graphics video memory, text video memory, etc. The near pointer is two bytes in size. We may make any pointer a close pointer by using the keyword near.
  • It's a pointer that only works within the 64Kb data section of memory.
  • It is unable to access addresses outside of the specified data segment.
  • An arithmetic operator can be used to increase or decrease a near pointer in the address range.

Program:

#include <stdio.h>

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

 

Output:

2

Far Pointer

A 32-bit far pointer can reach memory outside of the current segment. The compiler creates a segment register for segment address and another register for offset inside the current segment to make use of this.

In general, a Far pointer is thought to be a 32-bit pointer. It may, however, use the current segment to retrieve information stored outside the computer's memory. Although we usually need to allocate the sector register to hold the data address in the current segment to utilize this sort of pointer. In addition, another register must be set aside to hold offset inside the current segment.

Syntax:

<data type> far <pointer definition>

 

For example, 

char far *s;  

Some important points about Far Pointer are as follows:-

  • Only the offset component of the pointer is modified when it is increased or decremented.
  • It's a pointer that holds both the offset and the segment address that the pointer is comparing.
  • A far pointer address might be somewhere between 0 and 1MB.
  • All 16 segments are accessible.

Program:

#include <stdio.h>

int main()
{
    int num=20;  
    int far* ptr;  
    ptr=&num;  
    printf("%d",sizeof num);  
    return 0;  
}

 

Output:

4

Huge Pointer

Huge pointers have the same 32-bit size as far pointers and may reach bits outside the sector. Far pointers are fixed, and the sector in which they are situated cannot be changed; however, huge pointers may.

Like a far pointer, a huge pointer is usually 32 bits and may access the outer segment. A segment is fixed in the case of far pointers. The segment component of the far pointer cannot be changed; however, it can be changed in case of a huge pointer.

Some important points about Huge Pointer are as follows:-

  • A huge pointer is a pointer that may point to any section in the memory. A huge pointer is 4 bytes or 32 bits in size and can access up to 64K of memory.
  • Without suffering from segment work round, the huge pointer can be increased.

Program:

#include <stdio.h>

int main()
{
    char huge * far *ptr;  
    printf("%d %d %d",sizeof(ptr),sizeof(*ptr),sizeof(**ptr));  
    return 0;  
}

 

Output:

4 4 1

Frequently Asked Questions

What is a huge pointer?

Huge pointers have a 32-bit size pointer that may point to any section in the memory.

What is a near pointer?

near-pointer is used to hold 16-bit addresses within the current segment. It is two bytes in size.

What is a far pointer?

far pointer is thought to be a 32-bit pointer. It uses the current segment to retrieve information stored outside the computer's memory. 

Conclusion

In this article, we have extensively discussed the near, far, and huge pointer, their syntax, and program.

We hope this blog has helped you enhance your knowledge regarding the near, far, and huge pointers in C language. Some official documentation on C programming language that can help you improve your understanding is C documentation.

If you would like to learn more, check out our articles on pointerslearn pointers, and pointers in C

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Was this article helpful ?
0 upvotes