在C中,是否允许直接从某种类型的指针转​​换为另一种类型?

发布于 2024-12-10 19:23:52 字数 403 浏览 0 评论 0原文

假设您从一个 void 指针、一个 char 指针、一个 int 指针或任何您想命名的指针开始。

    void *p = // initialized to something here

我们进行类似的转换,

    *((int *)((char *)p + 6)) = 5;

这是否意味着我们基本上将 void 指针转换为 char 指针,进行一些算术,将其转换为 int 指针,然后取消引用它以存储 5?

或者我们是否需要先将 char 指针转换回 void 指针,然后才能安全地将其转换为 int 指针?

* 另外,在从 char* 转换为 int* 之前,是否需要在转换之前在某处取消引用?

Let's say you start with a void pointer, or a char pointer, or an int pointer, or whatever you would like to name.

    void *p = // initialized to something here

And we do a conversion like

    *((int *)((char *)p + 6)) = 5;

Does this mean we are basically casting a void pointer to a char pointer, doing some arithmetic, casting that to an int pointer, and then de-referencing it to store 5?

Or do we need to cast the char pointer back to a void pointer before it is safe to cast it to the int pointer?

* Also, before casting from char* to int*, does there need to be a de-reference somewhere before the conversion?

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

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

发布评论

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

评论(4

戈亓 2024-12-17 19:23:52

您显示的转换在语法上是有效的 C。通过 void * 类型转换为 int * 没有什么区别。

它在语义上是否正确取决于 (char *)p + 6 指向的内存是否正确调整大小并对齐以作为 int 进行访问。

The conversion you have shown is syntactically valid C. Casting through a void * type on the way to int * makes no difference.

Whether it is semantically correct depends on whether the memory pointed to by (char *)p + 6 is correctly sized and aligned for access as int.

蝶舞 2024-12-17 19:23:52

在将 char 指针转换为 int 指针之前将其转换回 void 指针并不比将 char 指针转换为 int 指针更好。不过,您必须真正了解自己在做什么才能使其发挥作用,因为您必须处理对齐问题。

Casting a char pointer back to a void pointer before casting it to an int pointer won't be any better than just casting a char pointer to an int pointer. You've got to really know what you are doing to make it work though, because you have to deal with alignment issues.

半仙 2024-12-17 19:23:52

C 在允许潜在危险的指针类型转换方面比 C++ 宽松得多。它源于 C 的“程序员知道他们在做什么”的哲学。然而,由于类型转换的潜在危险,C++ 引入了三种类型的转换:static_castdynamic_castreinterpret_cast

推荐阅读:

  1. http://www.crasseux.com/books/ctutorial/Pointer- types.html
  2. http://www.cplusplus.com/doc/tutorial/typecasting/

C is much more lenient than C++ in allowing potentially dangerous pointer typecasts. It stems from C's "the programmer knows what they're doing" philosophy. However, because of the potential dangers of typecasting, C++ introduces three types of casting, static_cast, dynamic_cast, and reinterpret_cast.

Recommended reading:

  1. http://www.crasseux.com/books/ctutorial/Pointer-types.html
  2. http://www.cplusplus.com/doc/tutorial/typecasting/
凌乱心跳 2024-12-17 19:23:52

从一个指向一种整数类型的指针转​​换为另一个指向不同整数类型的指针如果导致对齐问题,则属于未定义行为 (C99 6.3.2.3)。如果没有,您可以在它们之间安全地进行转换。 char 是整数类型。

(C 标准在这里有点奇怪,指出这是 UB。这应该是实现定义的行为,因为它取决于特定的 CPU 架构并且在许多情况下是安全的。)

在您的具体示例中,它确实是你所描述的意思。其背后的原因可能是:

  • 您无法对 void 指针执行指针算术。因此,它被类型转换为 char*,以便地址可以更改为原始的 1 + 6*sizeof(char),即 6 个字节。
  • 希望程序员能够意识到对齐,并且将特定地址处的任何内容作为 int 进行寻址是安全的。如果是这样,则类型转换为 int* 是完全安全的。

Casting from a pointer to one integer type, to another pointer to a different integer type is undefined behavior if it leads to alignment issues (C99 6.3.2.3). If it doesn't, you can cast between them safely. char is an integer type.

(The C standard is a bit weird here, stating that this is UB. This should have been implementation-defined behavior, because it depends on the specific CPU architecture and is safe in many cases.)

In the case of your specific example, it does indeed mean what you describe. The reasons behind it are likely:

  • You can't perform pointer arithmetics on a void pointer. Therefore it is typecasted to a char* so that the address could be altered to the original one + 6*sizeof(char), ie 6 bytes.
  • Hopefully the programmer is aware of alignment and that it is safe to address whatever is at that specific address as an int. If so, it is perfectly safe to typecast to an int*.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文