Let us learn about Far pointer in C programming and understand how it is implemented with an example, explanation, advantages, disadvantages and much more.
Why were Far Pointers used?
The old days witnessed very small sized memory chips within the CPU because of the huge costs involved in it.
The size of the RAM and the CPU address space were much smaller as compared to what we have now. There were different types of memory efficient pointers such as near, far and huge pointers.
However, far pointers are not used these days as we have much better CPU memory today. They were a part of Disk Operating System (DOS).
You can implement far pointer in C programming using a 16-bit compiler such as Turbo C, Turbo C++ but not in modern compilers such as C4Droid, GCC, etc.
What is a Far pointer?
In simple words, if a pointer to an object can access all the different 16 segments of a Random Access Memory (RAM), then it is regarded as a far pointer.
There are different blocks of memory and every block can store particular bytes of data within it. If the data requires to be allocated to multiple blocks, and if the pointer can access all the memory blocks then it is a far pointer.
A pointer which can point to any segment in the memory is known as a far pointer. A far pointer has a size of 4 bytes or 32-bits.
Image Source: Wikipedia
In order to define any far pointer, it is important to use the non-standard qualifier far along with the pointer variable. A far pointer includes an explicit selector.
A far pointer consumes 32 kilobytes (4 bytes) of memory in your CPU. The first 16 bits stores segment value and the second 16 bits stores offset value. They may reference up to 1024 KB or 1088 KB of memory.
The first word in a far pointer contains the 14-bit memory offset (bits 14 and 15 are always 0). The second word consists of the page number (or segment number for function pointers).
The memory address is calculated as follows:
Function Address = (Segment Number * 0x10000L) + Offset
Variable Address = (Page Number * 0x4000L) + Offset
|Address + 0||Address + 1||Address + 2||Address + 3|
|Contents||Offset (LSB)||Offset (MSB)||Segment (LSB) or Page (LSB)||Segment (MSB) or Page (MSB)|
Disadvantages of Far Pointers
- We cannot modify the segment address of a particular far address by applying any arithmetic operation on it.
- A far pointer is comparatively slower in execution as compared to near pointers.
- When a far pointer is incremented or decremented, only the offset of the far pointer is actually incremented or decremented.
- The relational operators will work on far pointers only if the segment values of the pointers being compared are the same.
- A far pointer is never normalized. A normalized pointer is a one that has as much of the address as possible in the segment, meaning that the offset is never larger than 15.
Advantages of Far Pointers
- A far pointer can access any segment of a RAM memory including all the 16 segments.
- A far pointer can reference up to 1 MB of data.
Sample Code To Implement Far Pointer in C Programming
int a = 10;
int far *ptr_var;
*ptr_var = &a;
If you have any doubts about far pointer in C programming, let us know about it in the comment section. Find more about it on Wikicoding.
|MORE ON POINTERS|