Near Far And Huge Pointers

In C++, near, far, and huge pointers are concepts related to memory addressing in segmented memory models, particularly in older systems like DOS. These memory models were devised to overcome the limitations of the 16-bit architecture, where the available memory was divided into segments, each with a maximum size of 64KB.

  1. Near Pointers:
    • Near pointers are the most commonly used pointers in C++. They are 16-bit pointers that can access memory within the same segment as the data or code segment they belong to.
    • Near pointers provide efficient memory access within a single segment, making them suitable for most applications.
    • They are implicitly assumed in most C++ programs and are the default pointer type in modern systems.
  2. Far Pointers:
    • Far pointers are 32-bit pointers that can access memory beyond the current segment.
    • Unlike near pointers, which have a limited range within a single segment, far pointers can access data or code in any segment of the memory.
    • They are used when accessing data or functions located in different segments of memory, which was common in older systems with segmented memory models.
  3. Huge Pointers:
    • Huge pointers are similar to far pointers in that they can access memory beyond the current segment.
    • However, huge pointers use a different addressing scheme that allows them to access more memory than far pointers.
    • They are particularly useful for accessing very large data structures that span multiple segments.

Source Code Example with Line-by-Line Explanation:

Here’s a simple C++ code demonstrating the usage of near, far, and huge pointers:

#include <iostream>
#include <dos.h>

int main() {
    int near* nearPtr;
    int far* farPtr;
    int huge* hugePtr;

    // Assign memory addresses to pointers
    nearPtr = (int near*)0x1234;
    farPtr = (int far*)0x5678;
    hugePtr = (int huge*)0xABCD;

    // Accessing memory through near pointer
    std::cout << "Value at nearPtr: " << *nearPtr << std::endl;

    // Accessing memory through far pointer
    std::cout << "Value at farPtr: " << *farPtr << std::endl;

    // Accessing memory through huge pointer
    std::cout << "Value at hugePtr: " << *hugePtr << std::endl;

    return 0;
}

Line-by-Line Explanation:

  1. #include <iostream>: This line includes the necessary header file for input/output operations.
  2. #include <dos.h>: This line includes the header file for DOS-specific functions and declarations, which include definitions for near, far, and huge pointers.
  3. int main() {: Beginning of the main function.
  4. int near* nearPtr;, int far* farPtr;, int huge* hugePtr;: These lines declare pointers of different types: near, far, and huge.
  5. nearPtr = (int near*)0x1234;, farPtr = (int far*)0x5678;, hugePtr = (int huge*)0xABCD;: These lines assign memory addresses to the respective pointers, demonstrating how to initialize near, far, and huge pointers with specific memory addresses.
  6. std::cout << "Value at nearPtr: " << *nearPtr << std::endl;: This line accesses the memory pointed to by the near pointer and prints its value.
  7. std::cout << "Value at farPtr: " << *farPtr << std::endl;: This line accesses the memory pointed to by the far pointer and prints its value.
  8. std::cout << "Value at hugePtr: " << *hugePtr << std::endl;: This line accesses the memory pointed to by the huge pointer and prints its value.
  9. return 0;: End of the main function, returning 0 to indicate successful execution.

In this example, we demonstrate the usage of near, far, and huge pointers by assigning memory addresses to them and accessing memory through them. This illustrates how these different pointer types can be used to access memory in segmented memory models.

In modern programming, the concepts of near, far, and huge pointers are largely obsolete, as they were primarily used in segmented memory models that were common in older systems like MS-DOS. However, the following examples demonstrate similar concepts using more contemporary language features.

In segmented memory models, memory was divided into segments, and pointers were composed of both a segment address and an offset within that segment. Near pointers pointed to data within the same segment as the pointer itself, far pointers could point to data in any segment, and huge pointers could access more than 64KB of memory.

Near Pointer Example

#include <iostream>

int main() {
    int x = 10;
    int* nearPtr = &x;

    std::cout << "Value of x: " << *nearPtr << std::endl;

    return 0;
}

Explanation:

  • int x = 10;: Declares an integer variable x and initializes it to 10.
  • int* nearPtr = &x;: Declares a pointer nearPtr that points to the address of x.
  • std::cout << "Value of x: " << *nearPtr << std::endl;: Prints the value of x using the near pointer nearPtr.

This program demonstrates the concept of a near pointer, where the pointer and the data it points to are in the same segment.

Far Pointer Example

#include <iostream>

int main() {
    int x = 10;
    int far* farPtr = &x;

    std::cout << "Value of x: " << *farPtr << std::endl;

    return 0;
}

Explanation:

  • In modern systems, there is no direct equivalent to far pointers, as memory addressing is typically done uniformly across the entire address space.
  • Here, we simulate a far pointer by simply declaring a regular pointer farPtr

Huge Pointer Example

#include <iostream>
#include <vector>

int main() {
    std::vector<int> hugeArray(100000); // Creating a large array

    int* hugePtr = &hugeArray[0];

    std::cout << "Value of the first element: " << *hugePtr << std::endl;

    return 0;
}

Explanation:

  • In modern systems, where memory addressing is flat, there’s no direct equivalent to huge pointers.
  • Here, we simulate a huge pointer by using a regular pointer to access a large array, which might represent a huge block of memory.

These programs provide a conceptual understanding of near, far, and huge pointers, but in modern programming, their usage is largely historical and replaced by more sophisticated memory models.