- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
