Reinterpret_cast 在 C++ 中的使用
只是一个简单的问题,有这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答当前问题
不同之处在于他们做了两件完全不同的事情!
注意:您没有告诉我们
H
是什么,因此无法自信地回答这个问题。但一般原则适用。对于第一种情况是明智的代码,
H
应该是一个指向的指针(可能类型为
实例。您可以这样做是为了告诉编译器void*
?) fftw_complexH
实际上是一个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 asvoid*
possibly?) to afftw_complex
instance. You would do this to tell the compiler thatH
is really afftw_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 classfftw_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 regardingH
, 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 usestatic_cast
instead (which can also be easily searched for).这会转换 H 内部的指针类型(或者整数本身,如果 H 是整数类型),并告诉编译器“这是一个指针。别再想它是什么了,它现在是一个指针”。 H 用作存储类似指针的地址的地方。
这会将 H 的地址(无论 H 是什么类型的指针)转换为指向“fftw_complex”的指针。修改 H_cast 的内容现在将改变 H 本身。
如果 H 不是指针,则您需要第二个,如果是指针,则通常需要第一个。也有相反的用例,但它们不常见且丑陋(特别是重新解释 int 或 - 上帝禁止 - double 作为指针)。
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.
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).
指针强制转换始终作为reinterpret_cast 执行,因此当从void * 进行强制转换时,c 风格强制转换、static_cast 或reinterpret_cast 之间没有区别。
Reinterpret_casts 通常保留用于最丑陋的位置,其中 c 风格的强制转换和 static_casts 用于无害的强制转换。您基本上使用reinterpret_cast将某些代码标记为非常丑陋:
这样,这些丑陋的不安全转换是可搜索/可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:
That way, these ugly unsafe casts are searchable/greppable.