Cprogramming 简明教程

Near, Far, and Huge Pointers in C

诸如 near pointersfar pointershuge pointers 之类的概念在 C programming language 中用于处理分段内存模型。但是,这些概念在具有改进的 CPU architecture 的现代计算环境中已不再相关。

Concepts like near pointers, far pointers, and huge pointers were used in the C programming language to handle segmented memory models. However, these concepts are no longer relevant in modern computing environments with improved CPU architecture.

near, far, and huge pointers 的概念在16位英特尔架构中,在MS DOS操作系统时代得到实现。

The idea of near, far, and huge pointers was implemented in 16-bit Intel architectures, in the days of the MS DOS operating system.

Near Pointer

C中的“ near ”关键字用于声明一个 pointer ,它只能访问当前数据段内的内存。16位计算机上的 near pointer 是指针,它只能存储16位地址。

The "near" keyword in C is used to declare a pointer that can only access memory within the current data segment. A near pointer on a 16-bit machine is a pointer that can store only 16-bit addresses.

在特定时间内,一个 near pointer 只能访问大约 64 kb 的少量数据,这是它最主要的缺点。近指针的大小为 2 字节。

A near pointer can only access data of a small size of about 64 kb in a given period, which is its main disadvantage. The size of a near pointer is 2 bytes.

Syntax of Near Pointer

<data type> near <pointer definition>
<data type> near <function definition>

下述语句为变量“ptr”声明了一个近指针 -

The following statement declares a near pointer for the variable "ptr" −

char near *ptr;

Example of Near Pointer

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   // declaring a near pointer
   int near *ptr;

   // size of the near pointer
   printf("Size of Near Pointer: %d bytes", sizeof(ptr));

   return 0;
}

它将生成如下输出:

It will produce the following output −

Size of Near Pointer: 2 bytes

Far Pointer

一个 far pointer 是一个 32 位指针,它可以在给定段中访问 computer memory 之外的信息。要使用此指针,必须分配“段寄存器”来存储段中的数据地址,并且还必须在最近的段中存储另一个段寄存器。

A far pointer is a 32-bit pointer that can access information that is outside the computer memory in a given segment. To use this pointer, one must allocate the "sector register" to store data addresses in the segment and also another sector register must be stored within the most recent sector.

一个 far pointer 存储指针正在差异的偏移量和段地址。当指针增加或减少时,只有偏移量部分会改变。远指针的大小为 4 字节。

A far pointer stores both the offset and segment addresses to which the pointer is differencing. When the pointer is incremented or decremented, only the offset part is changing. The size of the far pointer is 4 bytes.

Syntax of Far Pointer

<data type> far <pointer definition>
<data type> far <function definition>

下述语句为变量“ptr”声明了一个远指针 -

The following statements declares a far pointer for the variable "ptr" −

char far *s;

Example of Far Pointer

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   int number=50;
   int far *p;

   p = &number;
   printf("Size of far pointer: %d bytes", sizeof(number));

   return 0;
}

它将生成如下输出:

It will produce the following output −

Size of far pointer: 4 bytes

Huge Pointer

一个 huge pointer 具有与 far pointer 相同的 32 位大小。巨大的指针还可以访问位于段之外的位。

A huge pointer has the same size of 32-bit as that of a far pointer. A huge pointer can also access bits that are located outside the sector.

远指针是固定的,因此它们所在的段的部分不能以任何方式修改;然而,可以修改巨大的指针。

A far pointer is fixed and hence that part of the sector in which they are located cannot be modified in any way; however huge pointers can be modified.

在巨大的指针中,偏移量和段地址都会改变。这就是为什么我们可以使用 huge pointer 从一个段跳转到另一个段。当它们比较绝对地址时,可以在其上执行关系运算。巨大的指针的大小为 4 字节。

In a huge pointer, both the offset and segment address is changed. That is why we can jump from one segment to another using a huge pointer. As they compare the absolute addresses, you can perform the relational operation on it. The size of a huge pointer is 4 bytes.

Syntax of Huge Pointer

以下是声明巨大指针的语法 -

Below is the syntax to declare a huge pointer −

data_type huge* pointer_name;

Example of Huge Pointer

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   int huge* ptr;
   printf("Size of the Huge Pointer: %d bytes", sizeof(ptr));

   return 0;
}

它将生成如下输出:

It will produce the following output −

Size of Huge Pointer: 4 bytes

Pointers to Remember

在使用近、远和巨大指针时,请记住以下几点 -

Remember the following points while working with near, far, and huge pointers −

  1. A near pointer can only store till the first 64kB addresses, while a far pointer can store the address of any memory location in the RAM. A huge pointer can move between multiple memory segments.

  2. A near pointer can only store addresses in a single register. On the other hand, a far pointer uses two registers to store segment and offset addresses. The size of a near pointer is 2 bytes, while the size of far and huge pointers is 4 bytes.

  3. Two far pointer values can point to the same location, while in the case of huge pointers, it is not possible.

近、远和巨大指针用于根据分段内存架构中的 segment registers 来管理内存访问。现代系统使用平面内存模型,其中内存被寻址为一个连续的空间。现代 C 编译器提供了更好的内存管理技术,这些技术不依赖于分段概念。

The near, far, and huge pointers were used to manage memory access based on segment registers in segmented memory architectures. Modern systems use flat memory models where memory is addressed as a single contiguous space. Modern C compilers provide better memory management techniques that don’t rely on segmentation concepts.