两个指针之间的转换/转换

发布于 2024-12-08 01:35:12 字数 353 浏览 3 评论 0原文

最近我做了很多关于文件流的练习。当我使用 fstream.write(...) 时 例如,编写一个包含 10 个整数的数组 (intArr[10]),我会这样写:

fstream.write((char*)intArr,sizeof(int)*10);

(char*)intArr 强制转换安全吗?直到现在我才遇到任何问题,但我了解了 static_cast (c++ 方式对吗?)并使用了 static_cast(intArr) 和它失败的!我无法理解......我应该改变我的方法吗?

Lately I've been doing a lot of exercises with file streams. When I use fstream.write(...)
to e.g. write an array of 10 integers (intArr[10]) I write:

fstream.write((char*)intArr,sizeof(int)*10);

Is the (char*)intArr-cast safe? I didn't have any problems with it until now but I learned about static_cast (the c++ way right?) and used static_cast<char*>(intArr) and it failed! Which I cannot understand ... Should I change my methodology?

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

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

发布评论

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

评论(1

信愁 2024-12-15 01:35:12

静态转换根本不是正确的事情。仅当相关类型自然可转换时,才能执行静态转换。但是,不相关的指针类型不能隐式转换;即,T* 通常不能与 U* 相互转换。您真正要做的是重新解释强制转换:

int intArr[10];

myfile.write(reinterpret_cast<const char *>(intArr), sizeof(int) * 10);

在 C++ 中,C 样式强制转换 (char *) 成为最合适的可用转换类型,其中最弱的是重新诠释的演员阵容。使用显式 C++ 样式转换的好处是可以证明您了解所需的转换类型。 (此外,没有与 const_cast 等价的 C 语言。)

也许注意到这些差异是有启发性的:

float    q  = 1.5;
uint32_t n  = static_cast<uint32_t>(q);      // == 1, type conversion
uint32_t m1 = reinterpret_cast<uint32_t>(q); // undefined behaviour, but check it out
uint32_t m2 = *reinterpret_cast<const uint32_t *>(&q); // equally bad

题外话:编写最后一行的正确方法有点复杂,但使用了大量的量铸造:

uint32_t m;
char * const pm = reinterpret_cast<char *>(&m);
const char * const pq = reinterpret_cast<const char *>(&q);
std::copy(pq, pq + sizeof(float), pm);

A static cast simply isn't the right thing. You can only perform a static cast when the types in question are naturally convertible. However, unrelated pointer types are not implicitly convertible; i.e. T* is not convertible to or from U* in general. What you are really doing is a reinterpreting cast:

int intArr[10];

myfile.write(reinterpret_cast<const char *>(intArr), sizeof(int) * 10);

In C++, the C-style cast (char *) becomes the most appropriate sort of conversion available, the weakest of which is the reinterpreting cast. The benefit of using the explicit C++-style casts is that you demonstrate that you understand the sort of conversion that you want. (Also, there's no C-equivalent to a const_cast.)

Maybe it's instructive to note the differences:

float    q  = 1.5;
uint32_t n  = static_cast<uint32_t>(q);      // == 1, type conversion
uint32_t m1 = reinterpret_cast<uint32_t>(q); // undefined behaviour, but check it out
uint32_t m2 = *reinterpret_cast<const uint32_t *>(&q); // equally bad

Off-topic: The correct way of writing the last line is a bit more involved, but uses copious amounts of casting:

uint32_t m;
char * const pm = reinterpret_cast<char *>(&m);
const char * const pq = reinterpret_cast<const char *>(&q);
std::copy(pq, pq + sizeof(float), pm);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文