Javatpoint Logo
Javatpoint Logo

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:

  • Near pointer
  • Far pointer
  • Huge pointer

Near pointer

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.

Syntax

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.

  • Near pointer cannot access beyond the data segment like graphics video memory, text video memory, etc. The size of the near pointer is two bytes. With the help of keyword near, we can make any pointer a near pointer.
  • It is a pointer that works within the range of the 64Kb data segment of memory.
  • It cannot access addresses beyond the given data segment.
  • A near pointer can be incremented or decremented in the address range by using an arithmetic operator.

To understand the "Near" pointer in more detail lets us see an example:

Example

Output

2

Far Pointer

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.

Syntax

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.

  • When the pointer is incremented or decremented, only the offset part is changed.
  • It is such a type of pointer that stores both offset and segment address to which the pointer is differencing.
  • A far pointer address ranges from 0 to 1MB.
  • It can access all 16 segments.

Example

Output

4

Huge pointers

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.

  • A pointer that 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.
  • The huge pointer can be incremented without suffering with segment work round.

The following-given example shows the usage of the huge pointer.

Example:

Output

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA