reinterpret_cast是什么二进制的? [英] What does reinterpret_cast do binary-wise?

查看:153
本文介绍了reinterpret_cast是什么二进制的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中写了一个记录器,我来到我想要记录日志并写入文件的部分。

I'm writing a logger in C++, and I've come to the part where I'd like to take a log record and write in to a file.

我创建了一个 LogRecord 结构,并希望将其序列化并以二进制模式将其写入文件。

I have created a LogRecord struct, and would like to serialize it and write it to a file in binary mode.

我已经阅读了一些有关C ++中序列化的帖子,其中一个答案包括以下代码片段:

I have read some posts about serialization in C++, and one of the answers included this following snippet:

reinterpret_cast<char*>(&logRec)



我试过阅读关于 reinterpret_cast

I've tried reading about reinterpret_cast and what it does, but I couldn't fully understand what's really happening in the background.

根据我的理解,它需要一个指针到我的结构,并将其变成一个指针的char,所以它认为,保存我的结构体的内存块实际上是一个字符串,是真的吗?

From what I understand, it takes a pointer to my struct, and turns it into a pointer to a char, so it thinks that the chunk of memory that holds my struct is actually a string, is that true? How can that work?

推荐答案

内存地址只是内存地址。内存不是固有的特殊 - 它只是一个巨大的字节数组,对于我们所有关心。

A memory address is just a memory address. Memory isn't inherently special - it's just a huge array of bytes, for all we care. What gives memory its meaning is what we do with it, and the lenses through which we view it.

指向结构的指针只是一个整数,它指定了一些偏移量内存 - 肯定你可以以任何你想要的方式处理一个整数,在你的情况下,作为指向一些任意数字字节的指针( char s)。

A pointer to a struct is just an integer that specifies some offset into memory - surely you can treat one integer in any way you want, in your case, as a pointer to some arbitrary number of bytes (chars).

reinterpret_cast()除了允许你将一个内存地址的视图转换为内存地址的另一个视图。

reinterpret_cast() doesn't do anything special except allow you to convert one view of a memory address into another view of a memory address. It's still up to you to treat that memory address correctly.

例如, char * 是传统的方式指向C ++中的字符串 - 但是类型 char * 字面意思是指向单个字符的指针。它是如何意味着一个指向空字符的字符串的指针?按照惯例,这是怎么回事。

For instance, char* is the conventional way to refer to a string of characters in C++ - but the type char* literally means "a pointer to a single char". How does it come to mean a pointer to a null-terminated string of characters? By convention, that's how. We treat the type differently depending on the context, but it's up to us to make sure we do so correctly.

例如,你如何知道要读多少字节?通过你的 char * 指向你的struct?类型本身给你零信息 - 它是由你知道你真的有一个面向字节的指针指向一个固定长度的结构。

For instance, how do you know how many bytes to read through your char* pointer to your struct? The type itself gives you zero information - it's up to you to know that you've really got a byte-oriented pointer to a struct of fixed length.

记住,在发动机罩,机器没有型号。一张纸不在乎你是否在每一行上写一篇文章,或者如果你在所有的东西上涂鸦。这是我们如何对待它 - 以及我们使用的工具(C ++)如何处理它。

Remember, under the hood, the machine has no types. A piece of paper doesn't care if you write an essay on each line, or if you scribble all over the thing. It's how we treat it - and how the tools we use (C++) treat it.

这篇关于reinterpret_cast是什么二进制的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆