c++ - SIGBUS 何时使用 Placement new?

标签 c++ linux g++

我有以下代码(已更新):

#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 整除。然后,我很困惑它期望什么样的对齐。还是其他原因?

提前致谢。

最佳答案

您似乎正在尝试在此处创建 文件并写入其中,因此最初它的大小为零,占用的内存页为零。但是 mmap 不能写入文件末尾,有效地为您分配内存:首先,它如何知道要向文件添加多少字节?您需要确保 /tmp/abc.txt 包含一些随后可以被 new 位置覆盖的字符。它不能追加

在我将大约 8 个随机字节写入 /tmp/abc.txt 后运行您的程序成功并覆盖这些

63 00 00 00 00 00 00 00

在我的 x86-64 上符合预期。该程序然后报告您可能打算生成的 msync 错误,并正常退出。

关于c++ - SIGBUS 何时使用 Placement new?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680215/

相关文章:

c++ - 帮助解释探查器结果 [STL]

c++ - 如何通过模板调用运算符重载?

c++ - 关闭c++之前关闭应用程序

c++ - 使用 memset 将其设置为 0 后字符串数组不可用

c - 如何以列表或数组的形式获取 $PATH 中的目录?

linux - 如何在/usr/test/only目录下的 'string'内搜索 'any file'

c++ - C++如何在成员变量中实例化子类的实例?

c++ - 简单的 OpenGL 应用程序

c++ - 如何在我的项目中将 *standard library* 函数/方法标记为已弃用(或完全禁用)?

Ubuntu 上的 R BayesVarSel 安装