> If I understand placement new. Object destruction requires an explicit
call to the destructor.
struct my_struct {
unsigned int val1 : 5;
unsigned int val2 : 4;
unsigned int reserved : 23;
};
Now given an address - say 0x40000000. For the case where I have bit
fields. What's the approach on the constructor/destructor here?
I suspect:
my_struct *m = new (0x40000000) my_struct;
// clean up?
Thanks in advance
Are you asking how to explicitly call the destructor. Its this way...
m->my_struct::~my_struct();
What approach for ctor/dtor?
There are 2 things here. When you say,
my_struct *m = new (0x40000000) my_struct;
Where did you get that address from? If that was obtained by dynamic
allocation, you'll have to free it yourself. ctor and dtor are only for
allocation, initialization and de-allocation, respectively of the
*members* of the struct. In your case, there's no dynamic allocation of
the members in your struct. So there's nothing that you need to do in
the destructor.
So, in essence, your code might look something like this...
void * ptr = malloc(some_size);
// lets assume that ptr has the value you'd specified (0x40000000)
my_struct *m = new (ptr) my_struct;
// ... use the object
m->my_struct::~my_struct(); // explicit dtor call
free(ptr); // free up the originally allocated memory
Oh btw - I assume that you've provided a placement new i your struct.
HTH
Srini