将双精度变量的地址转换为char*是什么意思?

huangapple go评论78阅读模式
英文:

When reading from a binary stream, what does it mean to cast the address of a double variable to a char*?

问题

以下是翻译好的部分:

我从一个在线的C++课程中获得了以下代码:

class tempStat
{
    public:
    // 数据成员。
    double minimum, maximum;
    
    // 构造函数。
    tempStat(double min = 0.0, double max = 0.0)
        : minimum(min), maximum(max)
    {}

    // 辅助方法,将tempStat对象以二进制格式写入文件。
    void write(std::ostream & os)
    {
        os.write((char*)&minimum, sizeof(double));
        os.write((char*)&maximum, sizeof(double));
    }

    // 辅助方法,从二进制文件中读取tempStat对象。
    void read(std::istream & is)
    {
        is.read((char*)&minimum, sizeof(double));
        is.read((char*)&maximum, sizeof(double));
    }
}

这个类的使用方式如下:

tempStat ts;
ts.read(ifile);

我的问题是,我们如何将一个内存地址(&minimum)强制转换为char指针?

其次,如果minimum是一个int,我们可能会写相同的代码(至少在(char*)&minimum部分),那么&someIntVariable怎么能被强制转换为char*呢?

英文:

I have the following code from an online C++ course:

class tempStat
{
    public:
    // Data members.
    double minimum, maximum;
    
    // Constructor.
    tempStat(double min = 0.0, double max = 0.0)
        : minimum(min), maximum(max)
    {}

    // Helper method, to write a tempStat object to a file in binary format.
    void write(std::ostream & os)
    {
        os.write((char*)&minimum, sizeof(double));
        os.write((char*)&maximum, sizeof(double));
    }

    // Helper method, to read a tempStat object from a file in binary format.
    void read(std::istream & is)
    {
        is.read((char*)&minimum, sizeof(double));
        is.read((char*)&maximum, sizeof(double));
    }
}

This class is used like so:

tempStat ts;
ts.read(ifile);

My question is, how can we cast a memory address (&minimum) as a pointer to a char?

Secondly, if minimum had been an int, presumably we'd have written the same code (at least, as far as (char*)&minimum), so how does it work that &someIntVariable can also be cast as a char*?

答案1

得分: 7

流中的信息以字节序列(char)的形式呈现。

您想要将该信息读入变量minimummaximum中,它们都是double类型的。

您假设您正在读取的字节构成了系统上double的有效字节表示。

您将double别名为字节序列(这是标准中允许的一种特殊规则;通常情况下,您不能假装某物是其他东西),然后将结果的char*传递给流的“read”函数。

流的“read”函数认为它被要求将数据放入char数组中,因为char*看起来像这样,而且该函数的设计也是如此。

因此,字节序列被“read”函数复制到您的double中。


对于write函数也是一样的。我们假装向流中提供了一个字节序列(char数组)以进行写入,我们被允许这样做,因为从本质上讲,double就是这样的。


其次,如果minimum是一个int,我们可能会写相同的代码(至少,在(char*)&minimum这一点上是如此),那么&someIntVariable如何也能被强制转换为char*呢?

因为int也是由一系列字节表示的。

每个对象都是。

英文:

The information in the stream is presented in the form of a sequence of bytes (chars).

You want to read that information into the variables minimum and maximum, which are doubles.

You assume that the bytes you're reading constitute a valid byte representation of a double on your system.

You "alias" the doubles as a sequence of bytes (this is permitted as a special rule in the standard; generally you can't just pretend something is something else), then give the resulting char* to the stream's "read" function.

The stream's "read" function thinks it's being asked to put the data into an array of char, because that's what a char* looks like, and because that's what the function is designed to do.

Thus, the sequence of bytes is copied by the "read" function, into your doubles.


It's the same with the write function. We're pretending to the stream that we're giving it a sequence of bytes (a char array) to write, and we're allowed to do that because that's fundamentally what a double is, underneath it all.


> Secondly, if minimum had been an int, presumably we'd have written the same code (at least, as far as (char*)&minimum), so how does it work that &someIntVariable can also be cast as a char*?

Because an int is represented by a sequence of bytes, too.

Every object is.

huangapple
  • 本文由 发表于 2020年1月6日 19:51:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定