是“0xffffffff00000000” 32 位和 64 位编译之间是否存在混淆?

发布于 2024-11-19 19:58:19 字数 1010 浏览 5 评论 0原文

我用64位编译了Qt。我的代码也是用 64 位编译的。我将(指针)成员变量初始化为零。当我检查它时,XCode 告诉我它的值不是 0,而是 0xffffffff00000000。

这是 32 和 64 混淆的迹象吗?当库和我的代码都有“g++ .. -arch x86_64 -Xarch_x86_64 ..”时,32 位初始化如何潜入可执行文件?如果有必要的话,我在雪豹上。

----Begin-Edit----

经过这么多年,我很高兴地发现,当将 0 分配给指针时,标准不会强加值 0x00..00,但这不是本案中的问题。

#include <stdio.h>

int main()
{
    const char * c = "Foo";
    printf("Pointers in this executable use %lu bytes.\n", sizeof(c));
    void * z = 0;
    printf("A zero pointer in this executable is %p\n", z);
}

如果我将上面的代码保存在“32_or_64.cpp”中,然后用“g++ -arch i386 32_or_64.cpp”编译它,我得到

Pointers in this executable use 4 bytes.
A zero pointer in this executable is 0x0

如果我用“g++ -arch x86_64 32_or_64.cpp”编译它,我得到

Pointers in this executable use 8 bytes.
A zero pointer in this executable is 0x0

如果你相信这个没有确定我的特定配置上的 0 不应该让我在 x86_64 中调试时看到精确的 0,请指出 出去。否则,辩论“null”是一场精彩的讨论,但在本线程中是无关紧要的。

----结束-编辑----

I compiled Qt in 64 bit. My code is also compiled in 64 bit. I initialize a (pointer) member variable to zero. When I inspect it, XCode tells me that its value is not 0 but 0xffffffff00000000.

Is this a sign of a mix-up between 32 and 64? How might the 32 bit initialization have crept into the executable when both the library and my code have 'g++ .. -arch x86_64 -Xarch_x86_64 .. '? In case it matters, I am on Snow Leopard.

----Begin-Edit----

I appreciate finding out after all these years that the standard does not impose the value 0x00..00 when one assigns 0 to a pointer, but this is not the issue in this case.

#include <stdio.h>

int main()
{
    const char * c = "Foo";
    printf("Pointers in this executable use %lu bytes.\n", sizeof(c));
    void * z = 0;
    printf("A zero pointer in this executable is %p\n", z);
}

If I save the code above in '32_or_64.cpp' then compile it with 'g++ -arch i386 32_or_64.cpp', I get

Pointers in this executable use 4 bytes.
A zero pointer in this executable is 0x0

If I compile it with 'g++ -arch x86_64 32_or_64.cpp', I get

Pointers in this executable use 8 bytes.
A zero pointer in this executable is 0x0

If you believe that this does not establish that 0 on my particular configuration should not let me see precisely 0 when debugging in x86_64, please point it out. Otherwise, debating 'null' is a wonderful discussion, but an irrelevant one in this thread.

----End-Edit----

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

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

发布评论

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

评论(2

雨落星ぅ辰 2024-11-26 19:58:19

更新:根据 π 的编辑,这个解释似乎是虚假的。但无论如何你可能会发现它很有趣。


在类 C 语言中,在源代码中写为 0 的指针值只是指定 空指针。空指针是保证不指向任何对象的指针,它被定义为测试等于整数零,但它不需要具有与整数零相同的内部表示形式。空指针可以有多种表示形式,具体取决于体系结构,甚至取决于指针的类型。

使用 0 来表示“空指针”可能是一个不幸的约定; Steve Summit 的 C 编程语言常见问题解答 关于这个主题。

我认为 hexa 的评论证明了理解这一约定的难度。问题在于需要区分三个概念:

  • 空指针的概念:与指向任何对象的指针不同的指针。
  • 机器上空指针的表示(在某些情况下是地址0x00000000,但这不是您可以或不应该依赖的东西)。
  • 如何在类 C 语言中创建测试空指针(通过使用空指针常量,例如 0NULL)。

这是 C++ 标准,第 4.10 节:

空指针常量是计算结果为零的整数类型的整型常量表达式右值。空指针常量可以转换为指针类型;结果是该类型的空指针值,并且可以与指向对象的指针或指向函数类型的指针的所有其他值区分开来。相同类型的两个空指针值比较相等。

这保证了您可以使用常量 0 创建一个空指针,并通过与 0 比较来测试指针是否为空,但没有提及空值的机器表示指针。

Update: this explanation seems bogus in the light of π's edit. But you might find it interesting anyway.


In C-like languages, a pointer value written as 0 in the source code is just a convention for specifying a null pointer. A null pointer is a pointer that is guaranteed not to point to any object, and it is defined to test equal to the integer zero, but it doesn't need to have the same internal representation as the integer zero. Null pointers can have a variety of representations, depending on the architecture, or even on the type of the pointer.

The use of 0 to mean "null pointer" is perhaps an unfortunate convention; the level of confusion it causes is perhaps best indicated by the length of Steve Summit's C programming language FAQ on the subject.

hexa's comment is, I think, evidence of the difficulty of understanding this convention. The trouble is that there are three ideas to be separated:

  • The concept of a null pointer: a pointer that's distinct from a pointer to any object.
  • The representation of a null pointer on a machine (in some cases by the address 0x00000000, but that's not something you can or should rely on).
  • How you can create and test null pointers in C-like languages (by using a null pointer constant like 0 or NULL).

Here's the C++ standard, section 4.10:

A null pointer constant is an integral constant expression rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal.

This guarantees that you can create a null pointer using the constant 0, and test whether a pointer is null by comparison with 0, but says nothing about the machine representation of the null pointer.

有木有妳兜一样 2024-11-26 19:58:19

完全有可能您的 this 指针没有指向正确的内存。如果您的程序表现出其他未定义的行为,那么这很可能只是随机垃圾内存。

Perfectly possible that your this pointer is not pointing to the correct memory. If your program exhibits other undefined behaviour, then it's perfectly possible that this is just random garbage memory.

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