Near, Far, and Huge pointers in C language
In this article, we learn some of the old concepts that were usually used and implemented in the 16 bit Intel architectures in the earlier days of Ms-Dos, but they are not used much nowadays. Still, these concepts are very important because they are like the building block of current technology. However, on the basis of the Memory model and segment, pointers can be classified into three types, which are as follows:
In general, we can consider the near point as it is used to store the address, which has a maximum size of 16 bits only. Using the near pointer, we can not store the address with a size greater than 16 bits. However, we can store all other smaller addresses that are within the 16 bits limit. You may think that the 16 bits are a little small because we can only access 64kb of data at a time. Thus, it is considered one of the major disadvantages of the near pointer, which is why it is no longer commonly used. Lets us see some other important key points that can help us to understand the near pointer.
The syntax is as follows -
The following statement declares a near pointer for the variable s
Lets us see some important key points that can help us to understand the "Near" pointer.
To understand the "Near" pointer in more detail lets us see an example:
In general, Far pointer is typically considered as a pointer of 32 bits size. However, it can also access the information stored outside the computer's memory from the current segment. Although to use this type of pointer, we usually need to allocate the sector register to store the data address in the current segment. In addition, we also need to allocate another register to store offset within the current segment.
The syntax is given below:
The following statements declares a far pointer for the variable s
Lets us see some other important key points that can help us to understand the Far pointer.
Like the far pointer, the huge pointer also has the same size as 32 bits, but it can even access the bits stored or located outside the segment. However, the far pointer is quite similar to the Huge pointer, but it still has some advantages over the far pointer. For example, the Far Pointer which is fixed; thus, that part of the sector in which they are stored or located can not be changed or modified in any way. However, it is not the case with the Huge pointer because the Huge Pointer has the elasticity which allows it to do that.
Lets us see some other important key points that can help us to understand the "Huge" pointer.
The following-given example shows the usage of the huge pointer.
4 4 1
In the given program, p is the huge pointer, *p is the far pointer, and **p is the char type data variable.
What is the difference between near, far, and huge pointers?
In general, a virtual address is made up of a selector and an offset. A near-pointer does not have an explicit selector, while huge pointers have an explicit selector. When we perform pointer arithmetic on a far pointer, the selector is not modified, but it can be modified in the case of a huge pointer.
Note: These are non-standard keywords, also implementation-specific, and are irrelevant in modern platforms.
Next TopicMagic Number in C