Near Pointer in C Programming

 

Let us learn about near pointer in C programming and understand how it is implemented with an example, explanation, advantages, disadvantages and much more.

Why were Near 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 far, huge and near pointers.

However, near 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 near pointer in C programming using a 16-bit compiler such as Turbo C.

What is a Near pointer?

In simple words, if a pointer to an object holds a memory address within the same data segment in your CPU memory, then it is regarded as a near pointer.

There are different blocks of memory and every block can store particular bytes of data within it. If the data does not require to be allocated to multiple blocks, then it is a near pointer. However, the memory size should be less than or equal to 64 kilobytes.

A pointer which can point only to a 64-kilobyte data segment, code segment or stack segment is known as a near pointer. A near pointer has a size of 2 bytes or 16-bits. A near pointer cannot access beyond the data segment like text memory, graphics video memory, etc.

Near Pointer in C Programming Explained with Example Code

Image Source: Wikipedia

A near pointer cannot access beyond the data segment like text memory, graphics video memory, etc. A near pointer only stores the offset of the address it is referring to. A near pointer includes an implied selector.

   

In order to define any near pointer, it is important to use the keyword near along with the pointer variable. It uses offset data address in a segment default.

Memory Consumption

A near pointer consumes 16 kilobytes (2 bytes) of memory in your CPU.

In most of the memory models, near pointers contain a 14-bit offset in bits 0-13 and a 2-bit DPP selector in bits 14-15.

   

Address + 0Address + 1
ContentsOffset (LSB)Offset (MSB)

Disadvantage of Near Pointers

The limitation of a near pointer is that it can only access a data segment of 64 kilobytes.

Advantage of Near Pointers

The near pointers are faster in execution as compared to far and huge pointers. It generates a comparatively smaller version of the program code.

Operations on Near Pointers

A near pointer can be compared with each other using relational operators (>, <, <=, >=, ==).

You can perform increment and decrement operations on a near pointer. However, if a near pointer which has a value of 64k is incremented, it will result to 0. This concept is known as wrapping a pointer.

Sample Code To Implement Near Pointer in C Programming

If you have any doubts about near pointer in C programming, let us know about it in the comment section. Find more about it on Answers.com.

MORE ON POINTERS
Null Pointers
Void Pointers
Wild Pointers
Dangling Pointers
Huge Pointers
Far Pointers
Function Pointers

Let's Discuss