C++ 远近指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3869830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Near and Far pointers
提问by Alok Save
What is difference between our usual pointers(ones which we normally use), near pointers and far pointers and is there a practical usage for near and far pointers in present day C/C++ systems? Any practical scenario which necessiates use of these specific pointers and not other c,c++ semantics will be very helpful.
我们常用的指针(我们通常使用的指针)、近指针和远指针之间有什么区别,在当今的 C/C++ 系统中近指针和远指针是否有实际用途?任何需要使用这些特定指针而不是其他 c、c++ 语义的实际场景都将非常有帮助。
回答by AndersK
The near and far keywords have their origin in the segmented memory model that Intel had before. The near pointers could only access a block of memory originally around 64Kb in size called a segment whereas the far pointers could go outside of that range consisting of a segment and offset in that segment. The near pointers were much faster than far pointers so therefore in some contexts it paid off to use them.
Near 和 far 关键字起源于英特尔之前的分段内存模型。近指针只能访问最初大约 64Kb 大小的内存块,称为段,而远指针可以超出该范围,该范围由段和该段中的偏移量组成。Near 指针比 far 指针快得多,因此在某些情况下使用它们是值得的。
Nowadays with virtual memory near and far pointers have no use.
现在有了虚拟内存,near 和 far 指针都没有用了。
EDIT:Sorry if I am not using the correct terms, but this is how I remembered it when I was working with it back in the day :-)
编辑:对不起,如果我没有使用正确的术语,但这是我在当天使用它时记住它的方式:-)
回答by Angelic C
Near and far is spoken in the context of machine architecture (x86, x64, 640kb + model of memory, x32 in mean of the protected mode and segmented, x86 in terms of segmented memory)
Near 和 far 是在机器架构的上下文中说的(x86、x64、640kb + 内存模型,x32 表示保护模式和分段,x86 表示分段内存)
Example: Given a system with 32 bit "long" addressing and 8-bit relative addressing. The relative distance would allow for at least 127 bytes in the forward (positive value) or previous (negative) direction. If the target is 1024 bytes away, a full 32-bit pointer must be used.
示例:给定一个具有 32 位“长”寻址和 8 位相对寻址的系统。相对距离将允许在前向(正值)或前向(负值)方向上至少有 127 个字节。如果目标距离 1024 字节,则必须使用完整的 32 位指针。
Nowadays only x86 x32 addressing - a long pointer and x64 64 bit addressing are having and taking (permanent) place. Yes, a x64 in windows is compiled by MS VC the way it "sees" still like x86 x32 application ONLY 4Gb of memory. So you do not know by the default the difference between near and far or some other addressation. BUT x64 target has the ability to address the memory using the other, long long type of pointer.
现在只有 x86 x32 寻址 - 长指针和 x64 64 位寻址具有并占据(永久)位置。是的,Windows 中的 x64 是由 MS VC 编译的,它“看到”的方式仍然像 x86 x32 应用程序,只有 4Gb 的内存。因此,默认情况下您不知道近和远之间的区别或其他一些寻址方式。但是 x64 目标能够使用另一种 long long 类型的指针来寻址内存。
As you see there is no practical scenario in MS VC if you do not "see" the variant of the x64 addressing over x32 in the different pointers. Just a hypotetics.
如您所见,如果您没有“看到”不同指针中 x32 上的 x64 寻址的变体,则 MS VC 中没有实际场景。只是一个假设。
Staying away from this practical uses are LP_STRING - long pointer towards the string and INT_PTR - integer holding a value of pointer.
远离这种实际用途的是 LP_STRING - 指向字符串的长指针和 INT_PTR - 持有指针值的整数。
First you must understand, that CPU and compiler accepts the set of pointers-to-use sizes. And only them. You can't find a way round them without additional compilers/linkers/hardware. INT_PTR - is just for storage of the pointers that does not exceed int in length (a value of pointer, where pointer length is not greater than int). and LP_STRING - is just long type of pointer of the compiler/processor (they both like abstractions and compiler is not equal in the behaviour to the compiler, as well as x64 can be Intel rarely used one and widely used AMD).
首先,您必须了解,CPU 和编译器接受使用指针大小的集合。而只有他们。如果没有额外的编译器/链接器/硬件,你就找不到绕过它们的方法。INT_PTR - 仅用于存储长度不超过 int 的指针(指针的值,其中指针长度不大于 int)。和 LP_STRING - 只是编译器/处理器的长指针类型(它们都喜欢抽象并且编译器在行为上与编译器不同,x64 可以是 Intel 很少使用且广泛使用的 AMD)。
ANY size of variable can be a pointer.
任何大小的变量都可以是指针。
You just need a practical example against AMD x64 and LLP (long long pointer).
您只需要一个针对 AMD x64 和 LLP(长长指针)的实际示例。