Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Near Pointer
2.1.
Syntax of Near Pointer in C
2.2.
Example of near Pointer in C
2.3.
C
3.
Far Pointer
3.1.
Syntax of Far Pointer in C
3.2.
Example of Far Pointer in C 
3.3.
C
4.
Huge Pointer
4.1.
Syntax of Huge Pointer in C
4.2.
Example of Huge Pointer in C
4.3.
C
5.
Difference between Far Pointer and Near Pointer
6.
Difference between Far Pointer and Huge Pointer
7.
Frequently Asked Questions
7.1.
How big is the far pointer?
7.2.
What is a huge pointer?
7.3.
What is a near pointer?
7.4.
What is a far pointer and where is it used?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Near, Far and Huge Pointers

Author Aditi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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
Near, Far and Huge Pointers in C

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

Also See, Sum of Digits in C and C Static Function.

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 of Near Pointer in C

<data type> near <pointer definition>

Example of near Pointer in C

  • C

C

#include <stdio.h>


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

Output:

2

 

A near pointer for the variable S is declared in the following statement

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.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 of Far Pointer in C

<data type> far <pointer definition>

Example of Far Pointer in C 

  • C

C

#include <stdio.h>

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

Output:

4

 

A Far pointer for the variable S is declared in the following statement

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.

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.

Syntax of Huge Pointer in C

In C, "huge pointers" don't have a distinct syntax defined by the language itself. However, the concept of "huge pointers" typically refers to far pointers in older versions of C, especially in the context of MS-DOS programming or segmented memory models.

Example of Huge Pointer in C

  • C

C

#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

 

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.


You can also read about the dynamic arrays in c, and Tribonacci Series , Short int in C Programming

Difference between Far Pointer and Near Pointer

Here are the differences between far and near with respect to:

1. Memory Access:

  • Near pointers access memory within the same segment as the data or code.
  • Far pointers can access memory across different segments, useful for accessing data beyond the 64KB limit in segmented memory models.

2. Size:

  • Near pointers are typically 16-bit on most platforms, allowing direct access to limited memory.
  • Far pointers are larger, often 32-bit or more, accommodating segmented memory addressing.

3. Segmentation:

  • Near pointers are limited to accessing memory within the same segment, restricting access to nearby memory.
  • Far pointers facilitate accessing memory in different segments, extending the range of accessible memory.

Difference between Far Pointer and Huge Pointer

Here are the differences between far and huge with respect to:

1. Scope:

  • Far pointers extend the addressable range beyond the default near pointers, accessing memory across different segments.
  • Huge pointers are a workaround used in some older compilers to manage memory models, typically equivalent to far pointers.

2. Implementation:

  • Far pointers are a standard feature in segmented memory models, supported by compilers for accessing data beyond the 64KB segment limit.
  • Huge pointers are a non-standard extension or workaround implemented by some compilers, offering similar capabilities as far pointers.

3. Usage:

  • Far pointers are commonly used in segmented memory models, especially in older programming environments like MS-DOS.
  • Huge pointers are less common and were primarily used in specific contexts where far pointers alone couldn't address extensive memory requirements, often in older or specialized systems.

Frequently Asked Questions

How big is the far pointer?

Far pointers are typically 32-bit or larger, accommodating segmented memory addressing.

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 and where is it used?

A far pointer accesses memory across segments, useful in segmented memory models like those in older environments such as MS-DOS programming.

Conclusion

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

Also read - AMD vs Intel

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!

Previous article
Why does C treat array parameters as pointers?
Next article
What is a Size of a Pointer in C?
Live masterclass