【发布时间】:2018-02-08 07:49:56
【问题描述】:
我有以下代码(已更新):
#include <iostream>
#include <cassert>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <new>
struct S {
uint32_t a;
uint32_t b;
};
int main() {
const auto fd = open("/tmp/abc.txt", O_RDWR | O_CREAT, S_IRWXU);
assert(fd > 0);
void* ptr = mmap(nullptr, sizeof(S), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
assert(MAP_FAILED != ptr);
std::cout << "address: " << ptr << std::endl;
auto tptr = new (ptr) S();
tptr->a = 99;
const auto rc = msync(&ptr, sizeof(S), MS_ASYNC);
std::cout << "msync returned " << rc << std::endl;
if (rc != 0) {
std::cout << "error code = " << errno << ": " << strerror(errno) << std::endl;
}
}
当我在 GDB 中运行它时,我得到了这个:
address: 0x7ffff7ff8000
Program received signal SIGBUS, Bus error.
0x0000000000400adb in main () at msync.cpp:20
20 auto tptr = new (ptr) S();
我看到有人提到内存对齐问题,我检查了 0x7ffff7ff8000 是否可以被 2、8、16、32 和 64 整除。然后,我很困惑它期望什么样的对齐。还是别的什么?
提前致谢。
【问题讨论】:
-
存在三个主要问题。首先是对齐:您需要确保指针与您尝试放置的类型正确对齐。第二个更正式:为了使用标准库中的“placement”placement new 运算符,您需要包含其标题,即
<new>。第三,您应该通过从new表达式返回的类型化指针访问对象,而不是转换原始指针。 -
感谢您的快速输入。我已经完成了你建议的#2 和#3。但仍然是同样的问题。对于#1,如何确保内存对齐(以可移植的方式)?我有点依赖 mmap 在页面中运行,因此它返回的地址将与页面对齐。