在C++ 64位环境下,是否存在new运算符的结果指针占用全部8个字节的情况?
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
我使用 Windows 10 OS Pc 并在环境调试 x64 模式下构建。我用的是Visual Studio2019.
示例代码
int * d = new int;
-内存-
&d is 30 45 0f 2f 12 02 00 00
其实在这种情况下,高2字节好像没有用到
有没有用到这部分的案例?
我正在尝试将内存使用计数放入这个空 space(2byte)。
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
没有适用于所有 64 位 CPU 架构的一般保证,即指针的某些字节未被使用。
在x86-64架构的最新实现中,要求虚拟地址的高16位是最高前位的副本,只留下48位用于地址 space。该规范允许在未来将其扩展到完整的 64 位(256 TB 应该对任何人都足够 不过……对吗?)。
利用 CPU 架构的“未使用”字节的能力取决于语言实现(操作系统、编译器、消毒程序...)是否已经使用它们。
I'm trying to put a memory usage count in this empty space(2byte).
使用指针的高字节是可能的,只要您知道它会限制您的程序到其他操作系统和 CPUs 的可移植性。考虑一下你是否关心你的程序在 10 年后是否仍然有效。
然而,在通过 x86-64 上的指针间接访问之前,您必须小心恢复高位,即使它们是“未使用的”(参见上面的原因)。
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
Apparently not on 64-bit Windows (yet!):
Virtual address spaces
The range of virtual addresses that is available to a process is called the virtual address space for the process. Each user-mode process has its own private virtual address space. For a 32-bit process, the virtual address space is usually the 2-gigabyte range 0x00000000 through 0x7FFFFFFF. For a 64-bit process on 64-bit Windows, virtual address space is the 128-terabyte range 0x000'00000000 through 0x7FFF'FFFFFFFF.
截至 2020 年 2 月 11 日 - link 中指定的日期,64 位 Windows 似乎整个高 2 字节都未使用。
当然,对系统的任何更新都有可能改变......
Linux does use the entire virtual address space range, however.
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
正如其他人所解释的那样,2020 年不会(实际上)。但是,请注意一些潜在问题。
这样的标记指针可能会使您的可执行文件变慢(在某些情况下 - 请进行基准测试),因为每次您想要取消引用您的标记指针时,您都需要在它之前做一些事情。
你的代码可能会变得更难读,更难被未来的贡献者维护
您的代码可能会禁用有趣的编译器优化
您的代码对其他操作系统(包括 Windows 或 Linux 的未来版本)和其他架构(例如 ARM 或 PowerPC)的可移植性可能要差得多
您的代码可能不会赢得太多内存 space。指向的数据(在 C++ 中,它的 sizeof
)可能有多个单词,特别是当它包含多个 standard C++ containers ore more than one pointer, so you would win maybe 10% of RAM only (and performance would be slower) since most of your C++ aggregate types (class
-es, struct
-s) have more than one word (as soon as you use virtual
, there is likely to be a hidden virtual method table 指针时。
你会花费更多的调试工作。
I'm trying to put a memory usage count in this empty space (2 bytes).
- 您可能会遇到 reference counter 溢出您使用的 16 位的情况。所以你可能需要检查这种情况,而这样的运行时检查会使你的可执行文件更慢。
所以在你那里,我会和你的客户或经理讨论这种方法,我不推荐它。
看来你想实现某种粗暴的garbage collection. Be sure to read the GC handbook, since there are other approaches (in particular, Cheney's algorithm). Read at least more about tracing garbage collection, and take inspiration from existing open source implementations (e.g. SBCL or Ocaml).
考虑阅读与垃圾相关的会议论文 collection 和编程语言实现问题 - 例如ACM SIGPLAN 次会议(特别是 PLDI)。
我使用 Windows 10 OS Pc 并在环境调试 x64 模式下构建。我用的是Visual Studio2019.
示例代码
int * d = new int;
-内存-
&d is 30 45 0f 2f 12 02 00 00
其实在这种情况下,高2字节好像没有用到
有没有用到这部分的案例?
我正在尝试将内存使用计数放入这个空 space(2byte)。
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
没有适用于所有 64 位 CPU 架构的一般保证,即指针的某些字节未被使用。
在x86-64架构的最新实现中,要求虚拟地址的高16位是最高前位的副本,只留下48位用于地址 space。该规范允许在未来将其扩展到完整的 64 位(256 TB 应该对任何人都足够 不过……对吗?)。
利用 CPU 架构的“未使用”字节的能力取决于语言实现(操作系统、编译器、消毒程序...)是否已经使用它们。
I'm trying to put a memory usage count in this empty space(2byte).
使用指针的高字节是可能的,只要您知道它会限制您的程序到其他操作系统和 CPUs 的可移植性。考虑一下你是否关心你的程序在 10 年后是否仍然有效。
然而,在通过 x86-64 上的指针间接访问之前,您必须小心恢复高位,即使它们是“未使用的”(参见上面的原因)。
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
Apparently not on 64-bit Windows (yet!):
Virtual address spaces
The range of virtual addresses that is available to a process is called the virtual address space for the process. Each user-mode process has its own private virtual address space. For a 32-bit process, the virtual address space is usually the 2-gigabyte range 0x00000000 through 0x7FFFFFFF. For a 64-bit process on 64-bit Windows, virtual address space is the 128-terabyte range 0x000'00000000 through 0x7FFF'FFFFFFFF.
截至 2020 年 2 月 11 日 - link 中指定的日期,64 位 Windows 似乎整个高 2 字节都未使用。
当然,对系统的任何更新都有可能改变......
Linux does use the entire virtual address space range, however.
In C++ 64-bit environment, is there a case where the result Pointer of the new operator uses all 8 bytes?
正如其他人所解释的那样,2020 年不会(实际上)。但是,请注意一些潜在问题。
这样的标记指针可能会使您的可执行文件变慢(在某些情况下 - 请进行基准测试),因为每次您想要取消引用您的标记指针时,您都需要在它之前做一些事情。
你的代码可能会变得更难读,更难被未来的贡献者维护
您的代码可能会禁用有趣的编译器优化
您的代码对其他操作系统(包括 Windows 或 Linux 的未来版本)和其他架构(例如 ARM 或 PowerPC)的可移植性可能要差得多
您的代码可能不会赢得太多内存 space。指向的数据(在 C++ 中,它的
sizeof
)可能有多个单词,特别是当它包含多个 standard C++ containers ore more than one pointer, so you would win maybe 10% of RAM only (and performance would be slower) since most of your C++ aggregate types (class
-es,struct
-s) have more than one word (as soon as you usevirtual
, there is likely to be a hidden virtual method table 指针时。你会花费更多的调试工作。
I'm trying to put a memory usage count in this empty space (2 bytes).
- 您可能会遇到 reference counter 溢出您使用的 16 位的情况。所以你可能需要检查这种情况,而这样的运行时检查会使你的可执行文件更慢。
所以在你那里,我会和你的客户或经理讨论这种方法,我不推荐它。
看来你想实现某种粗暴的garbage collection. Be sure to read the GC handbook, since there are other approaches (in particular, Cheney's algorithm). Read at least more about tracing garbage collection, and take inspiration from existing open source implementations (e.g. SBCL or Ocaml).
考虑阅读与垃圾相关的会议论文 collection 和编程语言实现问题 - 例如ACM SIGPLAN 次会议(特别是 PLDI)。