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