Table of Contents
Near, Far And Huge Pointers In C
In the past, older Intel processors had 16-bit registers, which couldn’t hold the full memory address because the address bus was wider. To work around this limitation, memory was divided into 64-kilobyte segments. Concepts like near pointers, far pointers, and huge pointers were used in the C programming language to handle these segmented memory models. However, these concepts are mostly outdated and not relevant in modern computing environments.
1. Near Pointer
A “Near Pointer” on a 16-bit machine can only store 16-bit addresses, limiting it to accessing memory within the current 64-kilobyte segment. This means it can only reach the first 64 kilobytes of data in the memory.
The size of the near pointer is 2 bites.
Syntax
pointer_type near * pointer_name;
Example
// 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
2. Far Pointer
A “far pointer” uses two 16-bit registers to store memory addresses, allowing it to access memory outside the current segment. The compiler allocates one register to store the segment address and another for the offset within the current segment. To get the actual address, the offset is added to the shifted segment address.
However, in a far pointer, you can’t change the segment part by incrementing or decrementing it; this only affects the offset. The size of a far pointer is 4 bytes.
A challenge with far pointers is that different pointer values may actually point to the same address, making pointer comparisons less reliable.
Syntax
pointer_type far * pointer_name;
Example
// 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
A “huge pointer” also uses two registers to store addresses, like a far pointer. However, there are key differences:
- In a huge pointer, both the offset and the segment address can be changed, allowing you to jump from one memory segment to another.
- Huge pointers always compare the absolute addresses, which means you can perform relational operations on them with confidence.
- The size of a huge pointer is 4 bytes, just like a far pointer.
Syntax
pointer_type huge * pointer_name;
Example
// 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
- A “far pointer” can store addresses for any location in RAM, while a “near pointer” is limited to the first 64 kilobytes of memory.
- A far pointer uses two registers to hold segment and offset addresses separately, while a near pointer uses just one register.
- The size of a far pointer is 4 bytes, whereas a near pointer is 2 bytes in size.
The difference between a Far Pointer and a Huge Pointer
- A “far pointer” can’t move between different memory segments; it’s limited to a single segment.
- “Huge pointers” can move between multiple memory segments.
- Two different far pointer values can point to the same memory location, while this is not possible with huge pointers
FAQ- Near, Far, And Huge Pointers In C
Q1. What are near-far and huge pointers in C?
Ans.
A “near pointer” doesn’t have a separate selector.
“Huge pointers” have a selector. When you do pointer arithmetic with a far pointer, the selector isn’t changed. However, with huge pointers, the selector can be modified.
Q2. How many bytes are occupied by near far and huge pointers?
Ans. Near Pointers: 2 bytes.
Far Pointers: 4 bytes (16-bit segment + 16-bit offset).
Huge Pointers: 4 bytes (used for extended memory access).
Q3. Are pointers 4 or 8 bytes?
Ans. In modern computing environments, the size of pointers does indeed depend on the target architecture.
On 64-bit architectures, pointers are typically 8 bytes.
On 32-bit architectures, pointers are usually 4 bytes
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together