The below program when compiled using gcc and executed on a 32-bit architecture returns 16 bytes. I am wondering why it is returning so as it is neither uses a long double member nor it is running on a 64-bit machine and there is no virtual functions defined to get extra bytes of memory and when calculating the size of double and int data member, the result comes around 12 bytes. So why this 16 bytes value?
class A {
public:
void* data;
};
void* operator new (size_t sz, A& obj) {
printf("Custom operator new called for %d bytes\n", sz);
return obj.data;
}
class foo {
public:
double a;
int x;
foo() { printf("foo constructor called (this=%p)\n", this); }
};
int main() {
A obj;
obj.data = malloc(sizeof(foo));
printf("Allocator data: %p\n", obj.data); // Allocator data: 0x4601a8
foo *f = new (obj) foo; // Custom operator new called for **16 bytes**
// foo constructor called (this=0x4601a8)
printf("foo allocated at %p\n", f); // foo allocated at 0x4601a8
}