Back to C Programming
2026-07-146 min read

C - Near, Far and Huge Pointers

Learn C - Near, Far and Huge Pointers step by step with clear examples and exercises.

Why This Matters

In this full guide, we delve into the intricacies of Near, Far, and Huge pointers in C programming. Understanding these pointer types can help you tackle specific scenarios, such as working with larger data structures or accessing memory across different segments. This knowledge is essential for competitive coding, system programming, debugging complex issues, and gaining a deeper understanding of the underlying architecture of various systems.

Prerequisites

Before diving into Near, Far, and Huge pointers, you should have a solid foundation in C programming concepts:

  • Familiarity with basic C data types (int, char, float, etc.)
  • Understanding of variables and memory allocation
  • Comprehension of pointers and dereferencing (*)
  • Proficiency with arrays in C
  • Knowledge of function calls and parameters
  • Adequate understanding of the stack and heap memory areas
  • Familiarity with dynamic memory allocation functions like malloc(), calloc(), and free()
  • Understanding of structures and unions in C

Core Concept

Regular Pointers

In C, a pointer is a variable that stores the memory address of another variable. By default, all pointers are "near" pointers, which means they can only access memory within the current segment (usually the stack). The type of the data stored at the pointed-to location determines the type of the pointer.

int num = 10;
int *ptr = # // ptr is a near pointer pointing to the memory address of num

Far Pointers

Far pointers, also known as segmented pointers, are used in older systems (like DOS) with a segmented memory architecture. In this setup, memory is divided into segments and offsets. Far pointers consist of a 16-bit segment address and a 32-bit offset address. This allows far pointers to access memory across different segments.

#include <stdio.h>
#include <dos.h>

int main() {
int num = 10; // resides in the data segment
far int *far_ptr = MK_FP(0, &num); // creates a far pointer pointing to num
printf("%d\n", *far_ptr); // prints the value of num using the far pointer
}

Far Pointers and Modern Systems

While it's technically possible to use far pointers in modern systems, they are rarely needed due to the flat memory model used in most modern operating systems. Using them may lead to complications and is generally not recommended.

Huge Pointers

Huge pointers are used in systems with more extensive memory (>64KB) and a flat memory model. In this setup, the entire memory is treated as one large segment, eliminating the need for segmented pointers. Instead of using far pointers, huge pointers are used to access memory across different segments or pages.

#include <stdio.h>
#include <alloca.h>

int main() {
int *ptr = (int *) alloca(1024); // allocates 1KB on the stack for ptr
printf("%p\n", ptr); // prints the memory address of ptr
}

Huge Pointers and Heap Allocation

To create a huge pointer pointing to memory allocated on the heap instead of the stack, use malloc() or another dynamic memory allocation function:

int *ptr = (int *) malloc(1024); // allocates 1KB on the heap for ptr

Worked Example

Let's create a simple program that demonstrates the use of near, far, and huge pointers.

#include <stdio.h>
#include <dos.h>

int global_var = 20; // global variable accessible by far pointer

void farFunction() {
int local_var = 30; // local variable not accessible by near or huge pointers
}

int main() {
int num = 10; // near pointer
far int far_num = global_var; // far pointer pointing to a global variable
int *ptr = (int *) alloca(1024); // huge pointer

printf("Num: %d\n", num);
printf("Far Num: %d\n", far_num);
printf("Huge Pointer Address: %p\n", ptr);
farFunction(); // demonstrates the use of a far function
}

Common Mistakes

  1. Forgetting to include necessary headers (#include , #include )
  2. Incorrectly using far and huge pointers in modern systems
  3. Not handling memory allocation errors with huge pointers
  4. Confusing near, far, and huge pointers due to their similar syntax
  5. Assuming local variables are accessible via far or huge pointers (only global variables can be accessed by far pointers)
  6. Failing to deallocate memory allocated with malloc() when using huge pointers in modern systems
  7. Not understanding the differences between near, far, and huge pointers in terms of their usage and limitations
  8. Incorrectly converting near pointers to far pointers or vice versa
  9. Ignoring the historical context and purpose of far and huge pointers
  10. Not properly handling memory segmentation when working with older systems that require far pointers

Practice Questions

  1. Write a program that demonstrates the use of a far pointer to access a local variable from another function. (Hint: pass the local variable as a parameter)
  2. How can you create a huge pointer pointing to memory allocated on the heap instead of the stack? (Answer provided earlier in the "Huge Pointers" section)
  3. What are the advantages and disadvantages of using far and huge pointers in modern systems? (Advantages: accessing memory across different segments or pages; Disadvantages: complications, rarely needed due to flat memory model, not recommended for modern systems)
  4. How can you pass a near pointer as an argument to a function that expects a far pointer? (Convert the near pointer to a far pointer using MK_FP before passing it as an argument)
  5. What happens when you use a huge pointer in a modern system without handling memory allocation errors? (Memory leaks may occur if the allocated memory is not deallocated properly)
  6. Write a program that demonstrates the usage of near, far, and huge pointers to access variables in different memory segments or pages.
  7. Explain the differences between stack, heap, and global memory areas in C programming.
  8. How can you create a structure containing far pointers in C? (Create a structure with members that are far pointers, then use MK_FP to convert near pointers to far pointers when necessary)
  9. What is the purpose of the offset field in the union definition when working with far pointers? (The offset field specifies the offset within a memory segment where the union member resides)
  10. How can you convert a far pointer to a near pointer or vice versa in C? (Use the FP_SEG() and FP_OFF() macros to extract the segment and offset values, then create a new near pointer with these values)

FAQ

  1. Why are far pointers used in older systems like DOS?

Far pointers were used in older systems like DOS because they allowed accessing memory across different segments, which was necessary due to the segmented memory architecture of those systems.

  1. Can I use far pointers in modern systems?

While it's technically possible to use far pointers in modern systems, they are rarely needed due to the flat memory model used in most modern operating systems. Using them may lead to complications and is generally not recommended.

  1. What is the difference between near, far, and huge pointers?

Near pointers can only access memory within the current segment (usually the stack). Far pointers consist of a 16-bit segment address and a 32-bit offset address, allowing them to access memory across different segments. Huge pointers are used in systems with more extensive memory (>64KB) and a flat memory model, eliminating the need for segmented pointers.

  1. How can I create a huge pointer pointing to memory allocated on the heap instead of the stack?

To create a huge pointer pointing to memory allocated on the heap, use malloc() or another dynamic memory allocation function:

int *ptr = (int *) malloc(1024); // allocates 1KB on the heap for ptr
  1. What happens when I use a huge pointer in a modern system without handling memory allocation errors?

When you use a huge pointer in a modern system without handling memory allocation errors, memory leaks may occur if the allocated memory is not deallocated properly.