,其二进制表示具有两个最低有效位 未设置,然后是。 如果你的意思是它是合法的对于一个实现指向void 的二进制表示*总是*有两个最不重要的 位未设置,然后是。 如果你问是否有可能编写一个假定 的C程序,那么也是如此,但同时规定这样的C 程序不能严格遵守。从指针到整数的转换 和返回是最好的实现定义,最坏的是未定义。 如果你一般都在询问它是否''这是一个好主意,然后 强调没有。你的程序将被链接到一个相对较小的一套b / b $ b b的情况下,它必须小心做低级操作,并且 这个增益值得怀疑。如果您需要使用 指针存储额外信息,请考虑为每个单独的指针或哈希表使用结构。 (解决如何将指针哈希到void的问题尽可能便宜 比直接操纵代表更有价值。) S. pete< pf ***** @ mindspring.com>写道: 它没有。 N869 6.2.5类型 [#27]指向无效应具有相同的表示形式和对齐要求作为指向字符类型的指针。 这就是标准所说的指向void的指示的所有内容。 (在我的标准副本(ISO / IEC 9899:1999(E)中),即段落 6.2.5 - #26,而不是#27。想知道改变了什么。) 指向malloc()等返回的void的指针更强lignment 要求,(见下文,可能是错误的来源假设),但不仅仅是任何无效指针。它在特定环境中可能是4 的倍数,但当然任何依赖于该事实 将使您的代码不可移植。 7.20.3 "如果分配成功,则返回的指针适当对齐 ,以便可以将其分配给指向任何类型的指针对象..." Hi,Assuming that a pointer to void is always a multiple of 4 provides anopportunity to use as tags the two bits that are always 0. Knowing thatseveral experts on the ISO99 C standard should be monitoring thisgroup, I would like to know if this assumption complies with thisstandard before using it in a project. Also, if this is the case, wheredoes the standard state this?Best regards,David.-- 解决方案It doesn''t.N8696.2.5 Types[#27] A pointer to void shall have the same representationand alignment requirements as a pointer to a character type.and that''s all that the standard says about the representationof pointer to void.--peteIf by "complies" you mean "is guaranteed by", then no, this is not the case.If you mean that it''s legal for an implementation to have pointers to voidfor which the binary representation has the two least significant bitsunset, then yes.If you mean that it''s legal for an implementation to have pointers to voidfor which the binary representation *always* has the two least significantbits unset, then yes.If you''re asking whether it''s possible to write a C program that assumeseither is the case, then also yes, but with the provision that such a Cprogram cannot be strictly conforming. Conversions from pointers to integersand back are implementation-defined at best and undefined at worst.If you''re asking in general whether it''s a good idea to do this, thenemphatically no. Your program will be chained to a relatively small set ofcircumstances, it will have to be careful doing low-level manipulations, andthe gain is questionable. If you need to store extra information with apointer, consider using a struct for each individual pointer or a hashtable.(Solving the problem of how to hash pointers to void as portably as possibleis still more rewarding than manipulating the representation directly.)S.It doesn''t.N869 6.2.5 Types [#27] A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.and that''s all that the standard says about the representationof pointer to void.(In my copy of the standard (ISO/IEC 9899:1999(E),) that is paragraph6.2.5 - #26, not #27. Wonder what changed.)A pointer to void returned by malloc(), etc., has stronger alignmentrequirements, (see below, probably the source of the wrongassumption,) but not just any void pointer. It may be a multiple of 4in a particular environment, but of course any dependency in that factwill make your code non-portable.7.20.3"The pointer returned if the allocation succeeds is suitably alignedso that it may be assigned to a pointer to any type of object..." 这篇关于将指针对齐到void的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-31 00:27