SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
这里的new
操作符是什么意思?
new
运算符后面的数字3
是什么意思?
最佳答案
此代码来自 LLVM's codebase .
有一个自定义 operator new
in scope它被用于placement-new初始化对象(参见 placement syntax)
void *User::operator new(size_t Size, unsigned Us) {
return allocateFixedOperandUser(Size, Us, 0);
}
这是一个玩具示例:
class SelectInst
{
public:
int x;
};
void *operator new(size_t Size, unsigned Us) {
^^^^^^^^^^^ 3 is passed here
^^^^^^^^^^^ allocation size requested
return ... // Allocates enough space for Size and for Us uses
}
SelectInst *Create() {
return new(3) SelectInst();
}
int main()
{
auto ptr = Create();
return 0;
}
具体来说,该代码用于调整分配的空间以适应其他数据。
关于c++ - 新(3)是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753974/