C Programs Tutorials | IT Developer
IT Developer

C Programming - C near, far and huge Pointers



Share with a Friend

C Programming - C near, far and huge Pointers

Near, Far, and Huge Pointers in C

In C, the concepts of near, far, and huge pointers were primarily relevant to older 16-bit systems (like DOS and early versions of Windows) which used segmented memory. These pointer types were used to differentiate how memory addresses were handled in a segmented architecture.

  1. Segmented Memory Model

In the segmented memory model, the memory space was divided into segments, each of which could be 64 KB in size. This was typical for systems like DOS running on Intel 16-bit processors. Pointers in such systems had to specify both the segment and the offset within that segment.

  • Near pointer: Refers to a pointer that contains only the offset within a single segment.
  • Far pointer: Refers to a pointer that contains both the segment and the offset.
  • Huge pointer: Refers to a pointer that contains both the segment and the offset, but it is normalized, meaning it can handle larger memory models by allowing automatic adjustment of the segment.

These terms are now mostly obsolete with modern 32-bit and 64-bit processors and operating systems, which use flat memory models where addresses are treated as linear and there is no need for segmentation. However, in historical contexts, they were useful for understanding how pointers worked on early systems.

  1. Near Pointers
  • Definition: A near pointer contains only a 16-bit offset value that points to memory within the same segment (64 KB).
  • Size: Typically a 16-bit value.
  • Usage: Near pointers are used to refer to memory locations within a single segment. This means they can only address a maximum of 64 KB of memory in the current segment.

C

// Example of a near pointer (older systems with segmented memory)

int *nearPtr;

In modern systems, near pointers are generally not used. They were specific to 16-bit DOS systems.

  1. Far Pointers
  • Definition: A far pointer contains both a 16-bit segment and a 16-bit offset, which allows it to point to a memory location anywhere in the 64 KB memory space (across different segments).
  • Size: Typically 32 bits (16-bit segment + 16-bit offset).
  • Usage: Far pointers are used when you need to access memory beyond the 64 KB segment limit. It combines the segment address and the offset to create a larger memory address.

C

// Example of a far pointer (older systems with segmented memory)

far int *farPtr;

  • Limitations: A far pointer still limits you to 16-bit addressing for the segment. However, it allows you to reference memory that is not within the current segment.
  1. Huge Pointers
  • Definition: A huge pointer is similar to a far pointer, but with an important difference: huge pointers are normalized. This means the offset is automatically adjusted to ensure that the pointer can work across larger memory spaces without overflow issues. The segment can be automatically adjusted when the offset exceeds the boundary of a segment.
  • Size: Typically 32 bits (16-bit segment + 16-bit offset), but normalized.
  • Usage: Huge pointers were used in systems where memory models allowed for addressing more than 64 KB, even across multiple segments. The normalized offset would help in navigating large memory spaces.

C

// Example of a huge pointer (older systems with segmented memory)

huge int *hugePtr;

  • Limitations: Similar to far pointers, but with additional functionality for working with larger memory spaces in certain systems.
  1. Modern Relevance of Near, Far, and Huge Pointers

These pointer types are mostly obsolete in modern 32-bit and 64-bit computing systems, which use a flat memory model. In modern systems, the segmented memory model is no longer in use, and addresses are typically treated as linear.

Why Modern Systems Don't Use These Pointer Types:

  • Flat Memory Model: Modern systems do not require segmentation. A pointer is simply a memory address that refers to a unique location in the computer's linear address space.
  • 64-bit Memory: Modern 64-bit systems can handle much larger memory spaces directly, without needing to break memory into segments.

In modern C programming, pointers simply refer to a memory address, and their size is usually 32 bits or 64 bits depending on the architecture.

  1. Summary:
  • Near Pointer: Refers to memory within the same segment. It's a 16-bit pointer, mainly used in 16-bit systems.
  • Far Pointer: Refers to memory in different segments. It's a 32-bit pointer (16-bit segment + 16-bit offset), used in older segmented memory models.
  • Huge Pointer: Similar to a far pointer, but normalized to handle larger memory addressing.

Note:

These pointer types are rarely encountered in modern C programming. Instead, C programming today uses regular pointers that point to locations in linear memory spaces (which could be heap, stack, or data segments), with no segmentation concerns.