使用偏移量写入 mmap 文件,可能处理 endianess
Writing to mmap file with an offset, possibly dealing with endianess
我正在实现一个函数,该函数写入给定偏移量的内存映射文件。代码的极简解释如下:
MappedFile::MappedFile(int numOfItems, char * fname){
pageSize = sysconf(_SC_PAGE_SIZE);
fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
// Stretch the file size to the size of the (mmapped) array
size_t newsize = numOfItems*sizeof(int) + 1; // + null character
if (lseek(fd, newsize-1, SEEK_SET) == -1)
{
//below is not good practice. See comment for explanation.
close(fd);
perror("Error calling lseek() to 'stretch' the file");
exit(EXIT_FAILURE);
}
if (write(fd, "", 1) == -1)
{
close(fd);
perror("Error writing last byte of the file");
exit(EXIT_FAILURE);
}
if((mapBuffer = (char *)mmap(NULL, sizeof(int) * numOfItems, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED){
perror("Error mapping file");
exit(0);
}
}
void MappedFile::setItem(int index, int value){
//have problem here
mapBuffer[index*sizeof(int)] = value;
}
int MappedFile::getItem(int index){
return *((int *)(mapBuffer + index*sizeof(int));
}
该类将大量整数映射到二进制文件中。问题setItem
,getItem
并不总是设置/获取相同的值。例如:
MappedFile intarr(2000, "Test.bin");
intarr.setItem(5, 220);
cout << intarr.getItem(5) << endl;
将打印220
.然而
MappedFile intarr(2000, "Test.bin");
intarr.setItem(5, 2200);
cout << intarr.getItem(5) << endl;
将打印152
.
我怀疑这与恩迪亚内在有关。我尝试了以下方法:
mapBuffer[index*sizeof(int)] = ntohl(value); //for setItem
return htonl(*((int *)(mapBuffer + index*sizeof(int))); //for getItem
但getItem
总是返回 0。
有什么建议吗?谢谢。
最大的问题是您将指向char
的指针视为指向int
的指针,而这些不是一回事。如果您希望映射的内存存储整数,请使用指向int
的指针并使用正常的数组索引。
所以像
int* mapBuffer = static_cast<int*>(mmap(...));
mapBuffer[someIndex] = someIntValue;
更准确地说,以下赋值将截断整数以适应char
:
mapBuffer[index*sizeof(int)] = value;
mapBuffer 是一个字符* - 所以你只能将 int 的低 8 位写入它。 十六进制中的 2200 是 0x898,152 是0x98,因此您丢失了上字节。将指针升级到 int* 并从 get/set 中删除 *sizeof(int)。
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何用数字处理log(0)
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- 错误处理.将系统错误代码映射到泛型
- 从文本文件中读取时钟时间和事件时间并进行处理
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 用于矢量处理的多个线程
- 对字符串进行排序时,在c++中处理sort()
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 处理除以零会导致<csignal>意外行为
- 是否可以在c++中处理字符串流中的各个元素
- 使用偏移量写入 mmap 文件,可能处理 endianess