是否使用char*作为字节在内存中的位置

Is char* used as the location of a byte in memory?

本文关键字:内存 位置 字节 char 是否      更新时间:2023-10-16

我知道这段代码的作用:将&x所指向的内存中的内容复制到文件中,为sizeof(double) bytes;读取文件并复制到&y所指向的内存中,sizeof(double) bytes

double x,y;
std::ofstream out( "abc.dat", std::ios::out | std::ios::binary);
out.write( reinterpret_cast<const char*>(&x), sizeof(double));
out.close();
std::ifstream in( "abc.dat", std::ios::in | std::ios::binary);
in.read( reinterpret_cast<char*>(&y), sizeof(double));
in.close();

据我所知,在做二进制IO时,文件和RAM之间的数据流应该以字节为单位。然后,&x地址应该作为字节位置传递:类似于out。写(reinterpret_cast(&x), sizeof(double))是有意义的。然而,c++没有定义类型byte,这里的char*可以理解为字节位置,我对吗?我注意到char的大小与一个字节相同,它们都是8位。

在c++中char的大小是1字节。

除此之外,尺寸更灵活一些。这就是为什么一些供应商提供保证大小的非标准扩展。例如,在微软的VC中有一个16位的WORD。

查看更多信息:http://en.cppreference.com/w/cpp/language/types