Far pointers and near pointers are concepts used in early computer programming, particularly in 16-bit operating systems like MS-DOS, where they relate to the address capabilities of pointers.
Near Pointer
- Address Capability: Near pointers can only access memory within the same segment. In 16-bit operating systems, this typically means that the memory address range they can access is limited to 64KB.
- Storage Size: Since near pointers only need to point within the same memory segment, they typically occupy 2 bytes (in 16-bit architecture) for storage.
- Usage Scenario: Used when accessing data within a limited memory segment, as it is more efficient because it directly stores the offset address without additional segment addressing.
Far Pointer
- Address Capability: Far pointers can access data in different memory segments. They store both the offset address and the segment address, allowing them to point to any location within the entire 16-bit address space (up to 1MB).
- Storage Size: Far pointers require additional storage space to accommodate the segment information, typically occupying 4 bytes (in 16-bit architecture), with 2 bytes for the segment address and another 2 bytes for the offset address.
- Usage Scenario: Used when accessing data across segments or data structures larger than 64KB.
Example Illustration
Assume in a 16-bit system, we have two arrays, one located within the 0x1000 segment and another starting at 0x2000 segment. If only near pointers are used, it is not possible to directly access the array in the 0x2000 segment from the 0x1000 segment. However, with far pointers, we can set the segment address of the pointer to 0x2000 and set the offset to the start of the array, enabling access to any data within any segment.
Current Applications
In modern operating systems and programming environments (such as 32-bit or 64-bit systems), the concept of segmentation has been replaced by a flat memory model, effectively obsoleting the use of far pointers and near pointers. Modern programming languages and compilers generally no longer distinguish between far pointers and near pointers, instead using a unified pointer model to simplify memory management and improve program compatibility and runtime efficiency.
Overall, the difference between far pointers and near pointers is primarily defined by their memory access range and implementation mechanism, which is no longer a common distinction in modern programming practices. However, understanding these concepts helps in comprehending some historical and design decisions in early computer science.