Cover Image for Near, Far, and Huge pointers in C language
148 views

Near, Far, and Huge pointers in C language

The concepts of “near,” “far,” and “huge” pointers are associated with segmented memory models that were prevalent in the early days of computing. These concepts are mainly relevant to 16-bit and earlier architectures and are largely obsolete in modern 32-bit and 64-bit systems. Nonetheless, it’s useful to understand these concepts historically.

  1. Near Pointers:
  • Near pointers refer to memory addresses within the same segment (a contiguous block of memory).
  • In the context of a segmented memory model, the segment address was implicit, and near pointers only contained the offset within that segment.
  • Near pointers were suitable for accessing data within the current segment, which was typically limited to 64KB in the 16-bit x86 real mode.
  • In modern systems with flat memory models (such as 32-bit and 64-bit), all pointers are effectively near pointers because there’s no segmentation.

Example of a near pointer:

C
int x = 42;
int *nearPtr = &x;
  1. Far Pointers:
  • Far pointers refer to memory addresses in a different segment.
  • In a segmented memory model, a far pointer contains both a segment and an offset.
  • They were used to access data that resided in a different segment of memory.
  • Accessing data using far pointers required special handling.

Example of a far pointer:

C
int x = 42;
int far *farPtr = &x;
  1. Huge Pointers:
  • Huge pointers are a variation of far pointers.
  • They are used to access data that might be located in a different segment but also might span multiple segments.
  • Huge pointers contain both a segment and an offset like far pointers.
  • They were mainly used in memory models where data could potentially cross segment boundaries.

Example of a huge pointer:

C
int x = 42;
int huge *hugePtr = &x;

It’s important to note that in modern systems with flat memory models (such as most 32-bit and all 64-bit systems), these distinctions are largely obsolete, and all pointers are effectively treated as near pointers. In these systems, you don’t need to worry about segmentation and can use pointers without distinguishing between near, far, or huge. Segmentations and their associated pointer types are primarily historical artifacts from earlier computer architectures.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS