指针是否保证>某个值?

发布于 2025-01-04 23:47:39 字数 321 浏览 3 评论 0原文

在C++中,当我执行new(甚至malloc)时,是否可以保证返回地址将大于某个值?因为......在这个项目中,我发现使用 0-1k 作为枚举非常有用。但如果有可能获得那么低的值,我不想这样做。我唯一的目标系统是 32 位或 64 位 CPU,操作系统为 window/linux 和 mac。

标准有关于指针的说明吗? Windows 或 Linux 是否说明了它们的 C 运行时以及最低内存地址(对于 RAM)是多少?

-编辑-我最终修改了我的new重载以检查地址是否高于>1k。如果没有,我调用 std::terminate 。

In C++ when i do new (or even malloc) is there any guarantee that the return address will be greater than a certain value? Because... in this project i find it -very- useful to use 0-1k as a enum. But i wouldn't want to do that if its possible to get a value that low. My only target systems are 32 or 64bit CPUs with the OS window/linux and mac.

Does the standard say anything about pointers? Does windows or linux say anything about their C runtime and what the lowest memory address (for ram) is?

-edit- i end up modifying my new overload to check if the address is above >1k. I call std::terminate if it doesn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤檠 2025-01-11 23:47:39

就标准而言,没有什么。但实际上,这取决于目标操作系统,例如 Windows 保留前 64kb 内存作为无人区(取决于构建,它是只读内存,否则它被标记为 PAGE_NOACCESS),而它使用上部 0x80000000+ 为内核内存,但可以更改,请参见 这个 & MSDN 上的

在 x64 上,你也可以使用地址的高位(目前只有 47 位用于地址),但这不是一个好主意,因为稍后它会改变,你的程序将会崩溃(制定标准的 AMD 也建议不要使用它)。

In terms of standard, there is nothing. But in reality, it depends on the target OS, windows for instance reserves the first 64kb of memory as a no-mans land (depending on the build it is read-only memory, else it is marked as PAGE_NOACCESS), while it uses the upper 0x80000000+ for kernel memory, but it can be changed, see this & this on MSDN.

On x64 you can also use the higher bits of the address (only 47bits are used for addresses currently), but its not such a good idea, as later on it will change and your program will break (AMD who set the standard also advise against it).

二手情话 2025-01-11 23:47:39

没有这样的保证。您可以尝试使用展示位置new 如果你需要非常具体的内存位置,但它有某些问题,你必须努力工作到避免。为什么不尝试使用带有整数键且以指针作为值的映射呢?这样您就不必依赖特定的内存地址和范围。

There's no such guarantee. You can try using placement new if you need very specific memory locations but it has certain problems that you'll have to work hard to avoid. Why don't you try using a map with an integer key that has the pointer as its value instead? That way you wouldn't have to rely on specific memory addresses and ranges.

彼岸花似海 2025-01-11 23:47:39

从理论上讲,不——指针甚至不能保证 > > 。 0。然而,实际上,作为一个无符号整数(不要忘记指针可能有一个高位“1”位),据我所知,没有一个系统的指针值小于大约 1000。对此依赖于“未定义的行为”。

In theory, no -- a pointer's not even guaranteed to be > 0. However, in practice, viewed as an unsigned integer (don't forget that a pointer may have a high-order "1" bit), no system that I know of would have a pointer value less than about 1000. But relying on that is relying on "undefined behavior".

过潦 2025-01-11 23:47:39

有效内存地址的来源没有标准;要编写安全的独立于系统的代码,您不能依赖某些地址(即使有轶事支持,您也永远不知道新的系统更新何时会改变)。

There is no standard for where valid memory addresses come from; to write safe system-independent code, you cannot rely on certain addresses (and even with anecdotal support, you never know when that will change with a new system update).

疏忽 2025-01-11 23:47:39

它是特定于平台的,因此我不鼓励依赖此类信息,除非您有充分的理由并且意识到可移植性、可维护性等的后果。NULL

保证始终为 0x0。如果我没记错的话,x86 将前 128 MB 地址空间保留为“NULL 等效”,因此有效指针不能采用此范围内的值。在 x64 上,有一些其他地址,您在实践中不应该遇到这些地址,至少对于现在。

至于为操作系统保留的地址空间,显然取决于操作系统。在 Linux 上,内核-用户空间划分是可以在内核中配置的,因此至少 3 个划分:1-3 GB、2-2 GB 和 3-1 GB 在 32 位系统上很常见。您可以在 kerneltrap 上找到更多详细信息。

It's very platform-specific, so I would discourage relying on this kind of information unless you have a very good reason and are aware of consequences for portability, maintainability etc.

NULL is guaranteed to be 0x0 always. If I recall correctly, x86 reserves the first 128 MB of address space as "NULL-equivalent", so that valid pointers can't take on values in this range. On x64 there are some additional addresses which you shouldn't encounter in practice, at least for now.

As for address space reserved for the operating system, it will clearly depend on the OS. On Linux, the kernel-user space division is configurable in the kernel, so at least the 3 splits: 1-3 GB, 2-2 GB and 3-1 GB are common on 32-bit systems. You can find more details on kerneltrap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文