将指针对齐到void

编程入门 行业动态 更新时间:2024-10-28 10:22:55
本文介绍了将指针对齐到void的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设指向void的指针总是4的倍数提供了一个 的机会,可以将两个位用作标记0.知道 ISO99 C标准的几位专家应该监控这个 组,我想知道这个假设是否符合这个 $ b在项目中使用它之前的$ b标准。另外,如果是这种情况, 的标准是这样吗? 祝你好运, 大卫。 -

解决方案

David Deharbe写道:

假设指向void的指针总是4的倍数提供了一个机会,可以用作标记,这两个位总是为0. 知道那个关于ISO99 C标准的几位专家应该对这个组进行监控,我想知道在项目中使用它之前这个假设是否符合这个标准。

它没有。 N869 6.2.5类型 [ #27]指向void的指针应具有相同的表示形式 和对齐要求作为指向字符类型的指针。 这就是全部标准说明了无效的指示 。 - pete

David Deharbe写道:

假设指向void的指针总是4的倍数提供了一个机会,可以使用始终为0的两个位作为标签。了解ISO99 C标准的几位专家应该监控这个组,我想知道这个假设是否存在在项目中使用之前,请遵守此标准。此外,如果是这种情况,标准是否说明了这一点?

如果通过遵守你的意思是由...保证,然后不是,情况并非如此。 如果你的意思是实现指向void的合法 ,其二进制表示具有两个最低有效位 未设置,然后是。 如果你的意思是它是合法的对于一个实现指向void 的二进制表示*总是*有两个最不重要的 位未设置,然后是。 如果你问是否有可能编写一个假定 的C程序,那么也是如此,但同时规定这样的C 程序不能严格遵守。从指针到整数的转换 和返回是最好的实现定义,最坏的是未定义。 如果你一般都在询问它是否''这是一个好主意,然后 强调没有。你的程序将被链接到一个相对较小的一套b / b $ b b的情况下,它必须小心做低级操作,并且 这个增益值得怀疑。如果您需要使用 指针存储额外信息,请考虑为每个单独的指针或哈希表使用结构。 (解决如何将指针哈希到void的问题尽可能便宜 比直接操纵代表更有价值。) S.

pete< pf ***** @ mindspring>写道:

David Deharbe写道:

假设指向void的指针总是4的倍数提供了一个机会,可以将两个位用作标记。总是0. 知道 ISO99 C标准的几位专家应该监控这个组,我想知道这个假设是否符合这个标准,然后再使用它一个项目。

它没有。 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 an opportunity to use as tags the two bits that are always 0. Knowing that several experts on the ISO99 C standard should be monitoring this group, I would like to know if this assumption complies with this standard before using it in a project. Also, if this is the case, where does the standard state this? Best regards, David. --

解决方案

David Deharbe wrote:

Hi, Assuming that a pointer to void is always a multiple of 4 provides an opportunity to use as tags the two bits that are always 0. Knowing that several experts on the ISO99 C standard should be monitoring this group, I would like to know if this assumption complies with this standard before using it in a project.

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 representation of pointer to void. -- pete

David Deharbe wrote:

Assuming that a pointer to void is always a multiple of 4 provides an opportunity to use as tags the two bits that are always 0. Knowing that several experts on the ISO99 C standard should be monitoring this group, I would like to know if this assumption complies with this standard before using it in a project. Also, if this is the case, where does the standard state this?

If 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 void for which the binary representation has the two least significant bits unset, then yes. If you mean that it''s legal for an implementation to have pointers to void for which the binary representation *always* has the two least significant bits unset, then yes. If you''re asking whether it''s possible to write a C program that assumes either is the case, then also yes, but with the provision that such a C program cannot be strictly conforming. Conversions from pointers to integers and 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, then emphatically no. Your program will be chained to a relatively small set of circumstances, it will have to be careful doing low-level manipulations, and the gain is questionable. If you need to store extra information with a pointer, consider using a struct for each individual pointer or a hashtable. (Solving the problem of how to hash pointers to void as portably as possible is still more rewarding than manipulating the representation directly.) S.

pete <pf*****@mindspring> wrote:

David Deharbe wrote:

Assuming that a pointer to void is always a multiple of 4 provides an opportunity to use as tags the two bits that are always 0. Knowing that several experts on the ISO99 C standard should be monitoring this group, I would like to know if this assumption complies with this standard before using it in a project.

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 paragraph 6.2.5 - #26, not #27. Wonder what changed.) A pointer to void returned by malloc(), etc., has stronger alignment requirements, (see below, probably the source of the wrong assumption,) but not just any void pointer. It may be a multiple of 4 in a particular environment, but of course any dependency in that fact will make your code non-portable. 7.20.3 "The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object..."

更多推荐

将指针对齐到void

本文发布于:2023-07-29 08:25:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239058.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   void

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!