Near, Far and Huge Pointers in C
Last Updated :
19 Dec, 2024
In older times, the intel processors had 16-bit registers, but the address bus was 20-bits wide. Due to this, CPU registers were not able to hold the entire address at once. As a solution, the memory was divided into segments of 64 kB size, and the near pointers, far pointers, and huge pointers were used in C to store these addresses.
Although they are not commonly used in 32-bit or 64-bit architecture. In this article, we will learn about the near, far and huge pointers so that we can understand them when working with legacy code or embedded systems.
Near, Far and Huge Pointers in C1. Near Pointer
The Near Pointer is used to store the 16-bit addresses. It means that they can only reach the memory addresses within the current segment on a 16-bit machine. That is why we can only access the first 64 kb of data using near pointers. The size of the near pointer is 2 bytes.
Syntax
type near * name;
Let's take a look at an example:
C
#include <stdio.h>
int main() {
// Declaring a near pointer
int near* ptr;
// Size of the near pointer
printf("Size: %d bytes", sizeof(ptr));
return 0;
}
Output
2 bytes
Near pointers are often used when the memory is divided into segments, but the program only needs to access data within the same segment.
Note: Most of the modern compiler will fail to run the above program as the concept of the near, far and huge pointers is not used anymore. You can use TurboC for testing the above code.
2. Far Pointer
A far pointer stores the address in two 16-bit registers that allow it to access the memory outside of the current segment. The compiler allocates a segment register to store the segment address, then another register to store offset within the current segment. The offset is then added to the shifted segment address to get the actual address.
- In the far pointer, the segment part cannot be modified as incrementing/decrementing only changes the offset but not the segment address.
- The size of the far pointer is 4 bytes.
- The problem with the far pointers is that they can have different offsets and segment address but may points to the same memory location. So, the pointer comparison is useless on the far pointers.
Syntax
type far * name;
Let's take a look at an example:
C
#include <stdio.h>
int main() {
// Declaring far pointer
int far* ptr;
// Size of far pointer
printf("Size: %d bytes", sizeof(ptr));
return 0;
}
Output
Size: 4 bytes
Far pointers are used to access data across different segments in memory, typically using a 32-bit address that stores both the segment and the offset.
3. Huge Pointer
The huge pointer also stores the addresses in two separate registers similar to the far pointer but its characteristics make it different from the far pointers:
- In the huge pointer, both offset and segment address can be changed. That is why we can jump from one segment to other.
- The huge pointers always compare the absolute addresses, so the relational operation can be performed on it.
- The size of the huge pointer is 4 bytes.
Syntax
type huge * name;
Let's take a look at an example:
C
#include <stdio.h>
int main() {
// Declaring the huge pointer
int huge* ptr;
// size of huge pointer
printf("Size: %d bytes", sizeof(ptr));
return 0;
}
Output
Size: 4 bytes
Explanation: Huge pointers are primarily used to manage very large amounts of memory that extend across multiple segments and may span large areas of memory.
Difference between Far Pointer and Near Pointer
Following are the difference between the far pointer and near pointer in C:
- The far pointer can store the address of any memory location in the RAM whereas the near pointer can only store till first 64kB addresses.
- The far pointer uses two registers to store segment and offset addresses separately whereas the near pointer can only store addresses in a single register.
- The size of the far pointer is 4 bytes where as the near pointer is 2 bytes wide.
Difference between Far Pointer and Huge Pointer
Following are the difference between the far pointer and huge pointer in C:
- The far pointer cannot move between different segments whereas the huge pointer can move between multiple memory segments.
- The two far pointer values can point to the same location while in the case of the huge pointer, it is not possible.
Conclusion
In summary, near, far, and huge pointers were used in older systems to access memory in segmented memory architecture. While they are not commonly needed in modern systems, understanding these pointers can be helpful when working with older software, embedded systems, or low-level memory management in C.
Similar Reads
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C. Accessing the pointer directly will just give us the address that is stor
9 min read
Pointer Arithmetics in C with Examples Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
Applications of Pointers in C Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
4 min read
Passing Pointers to Functions in C Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
2 min read
C - Pointer to Pointer (Double Pointer) In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
5 min read
Chain of Pointers in C with Examples Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
5 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
How to Declare a Pointer to a Function? A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
2 min read
Pointer to an Array | Array Pointer A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
5 min read
Difference between constant pointer, pointers to constant, and constant pointers to constants In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
3 min read