C++ 中的措辞是什么?标准允许 static_cast(malloc(N));去工作?
据我理解 5.2.9 静态转换中的措辞,唯一允许 void*
到对象指针转换的结果是当 void*
首先是逆转换的结果。
在整个标准中,有大量对指针表示形式的引用,并且 void
指针的表示形式与 char
指针的表示形式相同,等等,但它似乎从未明确表示转换任意 void
指针会产生一个指向内存中同一位置的指针,但类型不同,就像类型双关是未定义的,而不双关回到对象的实际类型。
因此,虽然 malloc 明确返回合适内存的地址等,但据我所知,似乎没有任何方法可以实际使用它,可移植。
As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void*
-to-object-pointer conversion is allowed is when the void*
was a result of the inverse conversion in the first place.
Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void
pointer being the same as that of a char
pointer, and so on, but it never seems to explicitly say that casting an arbitrary void
pointer yields a pointer to the same location in memory, with a different type, much like type-punning is undefined where not punning back to an object's actual type.
So while malloc
clearly returns the address of suitable memory and so on, there does not seem to be any way to actually make use of it, portably, as far as I have seen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++0x标准草案在5.2.9/13中有:
但还要注意,强制转换并不一定会产生有效的对象:
C++0x standard draft has in 5.2.9/13:
But also note that the cast doesn't necessarily result in a valid object:
C++03,§20.4.6p2
C99,§7.20.3.3p2-3
(尽管 C++03 是基于 C89,我只有 C99 可以引用,但是我相信这部分在语义上没有变化。)
根据这两个引号,malloc 分配一个未初始化的对象并返回一个指向它的指针,或者返回一个空指针。指向作为 void 指针的对象的指针可以转换为指向该对象的指针(C++03 §5.2.9p13 的第一句,在前面的答案中提到)。
这应该减少你所抱怨的“挥手”,但有人可能会说我正在“解释”C 对 malloc 的定义,如我所愿,例如,注意到 C 说“到分配的空间”而不是“到分配的对象”。对于那些人:首先要意识到“空间”和“对象”在 C 语言中是同义词,其次请向标准委员会提交一份缺陷报告,因为即使是我也不够迂腐,无法继续。 :)
我会给你一个怀疑的好处,并相信你被标准之间的交叉引用、交叉解释和有时令人困惑的集成所绊倒,而不是“空间”与“对象”。
C++03, §20.4.6p2
C99, §7.20.3.3p2-3
(Though C++03 is based on C89, I only have C99 to quote. However, I believe this section is semantically unchanged. §7.20.3p1 may also be useful.)
From these two quotes, malloc allocates an uninitialized object and returns a pointer to it, or returns a null pointer. A pointer to an object which you have as a void pointer can be converted to a pointer to that object (first sentence of C++03 §5.2.9p13, mentioned in the previous answer).
This should be less "handwaving", which you complained of, but someone might argue I'm "interpreting" C's definition of malloc as I wish, by, for example, noticing C says "to the allocated space" rather than "to the allocated object". To those people: first realize that "space" and "object" are synonyms in C, and second please file a defect report with the standard committees, because not even I am pedantic enough to continue. :)
I'll give you the benefit of the doubt and believe you got tripped up in the cross-references, cross-interpretation, and sometimes-confused integration between the standards, rather than "space" vs "object".
是的,确实如此。
当然有:
有一个小问题:它不适用于任何其他类型。 :(
Yes, indeed.
Of course there is:
There is one tiny problem : it does not work for any other type. :(