Reinterpret_cast 在 C++ 中的使用

发布于 2024-11-24 06:51:17 字数 352 浏览 0 评论 0原文

只是一个简单的问题,有这个:

fftw_complex *H_cast;
H_cast = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);

之间有什么区别

H_cast=  reinterpret_cast<fftw_complex*> (H); 

H_cast= reinterpret_cast<fftw_complex*> (&H); 

提前非常感谢

Antonio

Just a simple question,having this:

fftw_complex *H_cast;
H_cast = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);

what is the difference between:

H_cast=  reinterpret_cast<fftw_complex*> (H); 

and

H_cast= reinterpret_cast<fftw_complex*> (&H); 

Thanks so much in advance

Antonio

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

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

发布评论

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

评论(3

魂牵梦绕锁你心扉 2024-12-01 06:51:17

回答当前问题

不同之处在于他们做了两件完全不同的事情!

注意:您没有告诉我们 H 是什么,因此无法自信地回答这个问题。但一般原则适用。

对于第一种情况是明智的代码,H 应该是一个指向 的指针(可能类型为 void*?) fftw_complex 实例。您可以这样做是为了告诉编译器 H 实际上是一个 fftw_complex*,因此您可以使用它。

对于第二种情况是合理的代码,H 应该是一个类的实例,其内存布局与类 fftw_complex 的内存布局相同。我想不出有什么令人信服的理由让自己陷入这种境地,这很不自然。基于此,由于您没有向我们提供有关 H 的信息,我认为这几乎肯定是一个错误。

原始答案

主要区别在于,在第二种情况下,您可以在源代码中搜索reinterpret_cast(并希望确保每次使用都有明确的记录,并且是必要的邪恶)。

但是,如果您要从 void* 转换为另一种指针类型(这里是这种情况吗?),那么最好使用 static_cast (也可以轻松搜索它) )。

Answer to current question

The difference is that they do two completely different things!

Note: you do not tell us what H is, so it's impossible to answer the question with confidence. But general principles apply.

For the first case to be sensible code, H should be a pointer (typed as void* possibly?) to a fftw_complex instance. You would do this to tell the compiler that H is really a fftw_complex*, so you can then use it.

For the second case to be sensible code, H should be an instance of a class with a memory layout identical to that of class fftw_complex. I can't think of a compelling reason to put yourself in this situation, it is very unnatural. Based on this, and since you don't give us information regarding H, I think it's almost certainly a bug.

Original answer

The main difference is that in the second case you can search your source code for reinterpret_cast (and hopefully ensure that every use is clearly documented and a necessary evil).

However, if you are casting from void* to another pointer type (is this the case here?) then it's preferable to use static_cast instead (which can also be easily searched for).

不再见 2024-12-01 06:51:17
H_cast=  reinterpret_cast<fftw_complex*> (H); 

这会转换 H 内部的指针类型(或者整数本身,如果 H 是整数类型),并告诉编译器“这是一个指针。别再想它是什么了,它现在是一个指针”。 H 用作存储类似指针的地址的地方。

H_cast= reinterpret_cast<fftw_complex*> (&H); 

这会将 H 的地址(无论 H 是什么类型的指针)转换为指向“fftw_complex”的指针。修改 H_cast 的内容现在将改变 H 本身。

如果 H 不是指针,则您需要第二个,如果是指针,则通常需要第一个。也有相反的用例,但它们不常见且丑陋(特别是重新解释 int 或 - 上帝禁止 - double 作为指针)。

H_cast=  reinterpret_cast<fftw_complex*> (H); 

This converts the pointer-ish type inside H (or the integer itself, if H is an integer type) and tells the compiler "this is a pointer. Stop thinking whatever it was, it's a pointer now". H is used as something where you had stored a pointer-like address.

H_cast= reinterpret_cast<fftw_complex*> (&H); 

This converts the address of H (which is a pointer to whatever type H is) into a pointer to "fftw_complex". Modifying the contents of H_cast will now change H itself.

You'll want the second if H is not a pointer and usually the first if it is. There are use cases for the other way around but they're uncommon and ugly (especially reinterpreting an int or - god forbid - a double as a pointer).

仲春光 2024-12-01 06:51:17

指针强制转换始终作为reinterpret_cast 执行,因此当从void * 进行强制转换时,c 风格强制转换、static_cast 或reinterpret_cast 之间没有区别。

Reinterpret_casts 通常保留用于最丑陋的位置,其中 c 风格的强制转换和 static_casts 用于无害的强制转换。您基本上使用reinterpret_cast将某些代码标记为非常丑陋:

float f = 3.1415f;
int x = *reinterpret_cast<int *>(&f);

这样,这些丑陋的不安全转换是可搜索/可greppable的。

Pointer casts are always executed as a reinterpret_cast, so when casting from or to a void * there's no difference between a c-style cast, a static_cast or a reinterpret_cast.

Reinterpret_casts are usually reserved for the ugliest of locations where c-style casts and static_casts are used for innocuous casts. You basically use reinterpret_cast to tag some code as really-ugly:

float f = 3.1415f;
int x = *reinterpret_cast<int *>(&f);

That way, these ugly unsafe casts are searchable/greppable.

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