SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
What's the meaning of the new
operator here?
What's the meaning of the number 3
after the new
operator?
This code comes from LLVM's codebase . There's a custom operator new
in scope and it is being used to placement-new initialize the objects (cfr. placement syntax )
void *User::operator new(size_t Size, unsigned Us) {
return allocateFixedOperandUser(Size, Us, 0);
}
Here's a toy example:
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;
}
Specifically that code is being used to adjust the allocated space to fit additional data.
Suppose SelectInst
provides a user-defined placement operator new , which takes an int
as user-defined argument; the invocation syntax means use that user-defined placement operator new
for memory allocation. eg
class SelectInst {
public:
static void* operator new (std::size_t count, int args) {
// ~~~~~~~~
...
}
};
SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
// ~~~
You probably have custom operator new in SelectInst
or at global scope (similarly to placement new ):
struct SelectInst
{
SelectInst(/*...*/);
// ...
static void* operator new(std::size_t sz, int custom);
static void operator delete(void* ptr, int custom); // new counter part
};
or
void* operator new(std::size_t sz, int custom);
void operator delete(void* ptr, int custom); // new counter part
See last part of operator_new
On the doc , it says that this is the amount of memory to request in bytes. (If it wasn't overloaded) Here's this is a request of 3 *8 = 24 bit to store the objet in memory. Think of this at some sort of remnant of malloc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.