相关疑难解决方法(0)

使用虚拟继承的地址未对齐

以下显然有效的代码使用UndefinedBehaviorSanitizer sanitiser产生错位的地址运行时错误.

#include <memory>
#include <functional>

struct A{
  std::function<void()> data; // seems to occur only if data is a std::function
} ;

struct B{
  char data; // occurs only if B contains a member variable
};

struct C:public virtual A,public B{

};

struct D:public virtual C{

};

void test(){
  std::make_shared<D>();
}

int main(){
  test();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在macbook上编译和执行 clang++ -fsanitize=undefined --std=c++11 ./test.cpp && ./a.out 产生输出 runtime error: constructor call on misaligned address 0x7fe584500028 for type 'C', which requires 16 byte …

c++ memory-alignment virtual-inheritance sanitizer clang++

6
推荐指数
1
解决办法
1137
查看次数