是否使用char*作为字节在内存中的位置
Is char* used as the location of a byte in memory?
我知道这段代码的作用:将&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
在c++中char的大小是1字节。
除此之外,尺寸更灵活一些。这就是为什么一些供应商提供保证大小的非标准扩展。例如,在微软的VC中有一个16位的WORD。
查看更多信息:http://en.cppreference.com/w/cpp/language/types
相关文章:
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 如何在不等待检索的情况下获取C++中的内存位置?
- 向量的内存位置不连续
- 如何根据C++在同一内存位置重新初始化 C# 中的对象(还是自动完成)?
- 常量引用的内存位置
- C++强制变量到一个固定的内存位置
- 为什么未初始化的内存位置的值给出 -842150451 的值?
- 为什么存储在内存位置的值会发生变化?
- 在特定内存位置构造 c++ 对象
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 内存位置出现Microsoft C++异常:std::out_of_range
- 函数,返回变量c++占用的内存位置的大小
- 无法在 Opencv 中显示图像导致内存位置
- 从十六进制到双的转换始终导致内存位置的 std::out_of_range
- 错误 在测试.exe 0x76C9FD62时出现未经处理的异常:Microsoft C++异常:内存位置0x006FF8
- 错误:内存位置的 std::length_error
- C++不同内存位置中的默认数组值
- 为什么动态分配的两个变量的内存位置不是连续的?
- Boost.进程间内存位置
- 查找编译时构造类的内存位置