Let us learn about Huge pointer in C programming and understand its implementation with an example, explanation, advantages, disadvantages and much more.
Why were Huge Pointers used?
The olden days used to have small sized memory chips because of the huge costs involved in it.
The size of the RAM chip was much smaller as compared to what we have now. In order to make minimum use of memory, there were different types of memory efficient pointers such as near, huge and far pointers.
However, huge pointers are not used these days as we have much better CPU memory today. They were a part of Disk Operating System (DOS) and were primarily used on x86 processors.
You can implement huge pointer in C programming using a 16-bit compiler such as Turbo C++ but not in modern compilers such as GCC.
What is a Huge 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. A huge pointer is identical to a far pointer, except that they allow access to data objects larger than 64KB.
There are different blocks of memory and every block can store particular bytes of data within it. If the data requires being allocated to multiple blocks, and if the pointer can access all the memory blocks then it is a huge pointer.
A pointer which can point to any segment in the memory is known as a huge pointer. A huge pointer has a size of 4 bytes or 32-bits and it can access up to 64K size in memory.
Image Source: Wikipedia
In order to define any huge pointer, it is important to use a non-standard qualifier huge along with the pointer variable. A huge pointer includes an explicit selector.
When a huge pointer is incremented or decremented, both segment value and offset value gets changed. It is easy to access and modify the device driver memory, IVT, video memory by using a huge pointer.
A huge pointer consumes 32 kilobytes (4 bytes) of memory in your CPU. The first word includes a 16-bit memory offset whereas the second word consists of the segment number.
The memory address is calculated as follows:
Address = (Segment * 0x10000L) + Offset
|Address + 0||Address + 1||Address + 2||Address + 3|
|Contents||Offset (LSB)||Offset (MSB)||Segment (LSB)||Segment (MSB)|
Advantages of Huge Pointers
- The offset value of a huge pointer is less than 16 due to pointer normalization.
- A huge pointer is normalized when some arithmetic operation is performed on it.
- A huge pointer can access up to 64K of memory size.
Disadvantages of Huge Pointers
- A huge pointer is comparatively slower in execution as compared to near pointers.
- If there’s a requirement of accessing more than 64K memory size, then we might have to use xhuge pointers.
- A huge pointer is not normalized when an assignment operation is performed on it.
Sample Code To Implement Huge Pointer in C Programming
int a = 25;
int huge *ptr_var;
*ptr_var = &a;
printf("\nSize of Huge Pointer:\t%d", sizeof(ptr_var));
If you have any doubts about huge pointer in C programming, let us know about it in the comment section. Find more about it on Keil.
|MORE ON POINTERS|