Far Pointers in C programming.
Far Pointer: A far pointer stores the address in two 16-bit registers and a special type of pointer in C language that can access memory location outside of the current segment, typically the segment is considered as a pointer of 32 bits size i.e 4 byte.
It consists of both a segment part and an offset part, allowing it to reference memory locations in different segments.
"Far pointers are typically used in certain situations where a program needs to access memory beyond the default segment size or in older systems."
In general, far pointer in c can also access information stored outside the computer's memory from the current segment. However, to use this type of pointer, we typically need to allocate a sector register to store the data address in the current segment. Besides, we also need to allocate another register to store the offset in the current segment.
Far pointer important key points
• In a far pointer, the segment part cannot be changed because decrementing/incrementing only changes the offset but not the segment address.
• The size of the far pointer is 4 bytes.
• The far pointer in C can access all 16 segments.
• When the pointer is incremented or decremented, only the offset part is changed.
• The problem with far pointers is that the pointer values are different but point to the same address. Therefore, pointer comparison on a far pointer is useless.
• A far pointer address ranges from 0 to 1MB.
Syntax of Far Pointer in C:
pointer_type far * name_of_pointer;
C Program illustrate to find the size of far pointer
#include <stdio.h>
int main()
{
int far* fptr; // declaring far pointer
// Size of operator is used to find the size of far pointer
printf("Size of Far Pointer: %d bytes", sizeof(fptr));
return 0;
}
Output
Size of Far Pointer: 4 bytes
Advantages of far Pointer in C.
1. Accessing memory beyond the current segment:
Far pointers in c language allow the programmer to access memory locations beyond or outside the default segment size. This can be useful in situations where the program needs to manipulate data spread over multiple sections.
2. Efficient memory management:
In segmented memory models, far pointers can help optimize memory usage by allowing for more flexible memory allocation and deallocation strategies. This can be particularly beneficial in resource-constrained environments.
In fragmented memory models the far pointers can help to optimize memory usage by allowing more flexible memory deallocation and allocation strategies. This is one of the special advantage in resource-constrained environments.
3. Enhanced flexibility:
Far pointers in C language provide greater flexibility in memory addressing, enabling programmers to work with complex data structures spanning multiple segments. It can simplify the implementation of some algorithms and data processing tasks.
4. Compatibility with legacy systems:
Far pointers in C language are commonly used in older systems that rely on fragmented or segmented memory models. By supporting far pointers, C programs can maintain compatibility with legacy codebases and systems that still use segmented memory architectures.
Overall, while far pointers may not be commonly used in modern programming practices, they offer advantages in specific contexts where segmented memory models are employed. Understanding and using far pointers can be beneficial for developers working on systems that need memory management beyond the limitations of the flat memory model.
In modern programming practices far pointers are used, and offers advantages in certain situations where segmented memory models are used.