- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我遇到了这段关于arduino内存的代码
void EEPROM_writeDouble(int address, double value)
{
byte* p = (byte*)(void*)&value;
for (int i = 0; i < sizeof(value); i++)
{
EEPROM.write(address++, *p++);
}
}
让我感兴趣的是这行代码 byte* p = (byte*)(void*)&value;
完成但没有弄清楚或者也许。所以我们有一个 byte(char)
类型的指针,它采用 void*
类型的值的引用并将其转换为字节指针?
最佳答案
自 double*
转换以来至 byte*
不会是有效的 static_cast
或 const_cast
, C++ 将请求该转换的 C 风格转换解释为等同于 reinterpret_cast
. (我假设 byte
是 unsigned char
或 char
的类型定义。)
在最初的 C++98 标准和 C++03 标准中,基本上没有保证 reinterpret_cast
是什么会做,只是说结果是“实现定义的”。因此,一次强制转换可能会得到一个指向 double
的存储开头的指针。变量,但从技术上讲,您不能指望这一点。
另一方面,例子中的两次转换,来自double*
至 void*
然后从 void*
至 byte*
, 有效 static_cast
转换,因此 C 风格的转换都具有 static_cast
的行为.还有一个 static_cast
至或来自(非空)[cv] void*
一直保证void*
指向对象存储的开头,导致指向虚构的 byte
的指针重叠 double
的存储目的。 (并且从真正属于不同类型对象的存储中的 unsigned char
或 char
读取是严格别名规则的特定异常(exception),所以没关系。)
请注意,从 C++11 标准开始,reinterpret_cast
的行为,因此 C 风格的转换,更具体地用于某些类别的指针转换:([expr.reinterpret.cast]/7)
An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue
v
of type "pointer toT1
" is converted to the type "pointer to cvT2
", the result isstatic_cast<
cvT2*>(static_cast<
cvvoid*>(v))
if bothT1
andT2
are standard-layout types and the alignment requirements ofT2
are no stricter than those ofT1
, or if either type is void. Converting a prvalue of type "pointer toT1
" to the type "pointer toT2
" (whereT1
andT2
are object types and where the alignment requirements ofT2
are no stricter than those ofT1
) and back to its original type yields the original pointer value. The result of any other such pointer conversion is unspecified.
因此使用最近的标准模式,不再需要这两个类型转换。 (代码可能是较早编写的,或者可能仍需要在多个 C++ 版本下工作,或者可能只是使用旧习惯编写的。尽管使用 C++ 风格的转换来说明您真正想要的转换逻辑类型几乎总是比使用 C 风格的转换更好的主意。)
关于C++ double cast void 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738197/
我很确定我不会是第一个被这个绊倒的人,但我找不到答案。这是作业。 class Tier{} class Fisch extends Tier{} class Vogel extends Tier{}
在接收器开发人员指南中如下: Applications running on the Google Cast receiver device (receiver applications) are a
最近我制作了一个自定义本地 Flask 网站,用于从本地设备转换到连接到我的电视的 RPi4。有一个 GUI它也可以通过 GET 请求控制,这是转换工作的主要方式,实际上是通过 GET 请求将数据从
最近我因为某些原因重置了我的路由器,所以当我尝试将它连接到 chromecast 时,Chromecast 没有在其列表中列出我的 wifi 名称。 之前它工作正常,所以我选择其他并手动输入我的网络名
做这样的 Actor 阵容的好方法是什么? seq { yield (box "key", box "val") } |> Seq.cast 因为这看起来非常难看: seq { yield (box
我继承的遗留 Web 应用程序是在经典 ASP 中为新西兰乐施会定制编写的,它在用户提交的输入上运行字符串替换,删除字符串“cast”,大概是因为 cast 函数。 然而,这意味着我们的参与者都不能拥
我去了三星网站并下载了 Media Control SDK 包。当我在我的 Galaxy S3 Android 4.3 上使用 AllShare Cast 视频播放器时,我的智能电视(在同一网络中)出
如何在 BigQuery 中查看 INT64 的二进制表示形式?将其转换为 BYTES 类型也可以。 最佳答案 正如@Elliot Brossard 所述,INT64 如何执行是一个实现问题。类型表示
Google 的文档要求我们设置自己的网络服务器来托管 receiver.html。 是否有任何人都可以使用的默认公共(public) chrome-cast 接收器? 我只需要非常基本的接收器,与
我在 Eclipse 3.6 (Helios) 中运行某人的代码时遇到问题。代码是用 OpenJDK 7 编写的。我运行的是 Windows 7,我必须为它安装 Java 7。我之前使用的是 Java
所以我有一些 COM 类型,它们的名称很难记住、又长又笨重,所以如果可以避免的话,我宁愿在从对象进行转换时不必键入它们。使用 Seq.cast 它将推断所需的类型并根据需要进行强制转换。 这是一个用
假设您不关心编译器和机器的转换风格,以下之间是否存在明显差异: #include #include #include static int64_t tosigned (void *p) {
我的测试类 XString 有两个转换运算符。但是编译器不会为 fooA 使用显式转换 operator const wchar_t*()。为什么? class XString { public:
我有以下代码: T imageCollectionItem; // This checks if image actually exists, if it doesn't it gets the ne
这有点假设,因为我不太担心性能 - 只是想知道一般来说哪个选项实际上最快/最有效,或者是否没有任何区别。 假设我有以下支持重载的访问者模板代码: #define IMPLEMENT_VISITOR_W
我目前正在存储复选框的真/假状态。在注册表中检查值以在下次加载表单时重置。 加载表单时,我获取值并像这样设置复选框。 string value = (string)Registry.GetValue(
enum Gender { Male, Female } var k = new[] { Gender.Male }.Cast().ToList().Cast().ToList(); //alrigh
我正在按照 https://codelabs.developers.google.com/codelabs/cast-receiver#0 上的教程构建一个 chrome cast 自定义接收器。我无
我看不懂以下作品 SELECT price, CAST(price AS DECIMAL(10,2)) FROM orders; 但是更新失败: UPDATE orders SET price = C
在 Cast v3 中,如何手动更改“停止转换”按钮的颜色? 我可以为其他所有内容更改转换对话框或媒体路由器的样式,但“停止转换”按钮似乎会根据背景颜色切换为黑色/蓝色。 我的主题:
我是一名优秀的程序员,十分优秀!