placement new and bit fields.

M

ma740988

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
 
S

Srini

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
 
M

ma740988

|| m->my_struct::~my_struct();

For starters, I suspect it's leagal to put destructors in bit fields...
so now:

struct my_struct {
unsigned int val1 : 5;
unsigned int val2 : 4;
unsigned int reserved : 23;
~my_struct();
};

Thats ok?

Now... The address I'll use is obtained from a vendor function. So
now:

unsigned int addr0;
STATUS stat;
// later
stat = sysPciConfigRead(PCI_CFG_BASE_ADDRESS_0, 4, &addr0);

Now addr0 is 'valid'. The contents at addr0 is really a a register
definition that looks like my_struct above..
To simpify things .. I thought well this is a candidate for
placement new:

my_struct *m = new ((void*)addr0) my_struct;

In a case like this, my guess is I really dont need to 'worry' abotu
'freeing' anything per se... so I might be ok..
 
S

Srini

For starters, I suspect it's leagal to put destructors in bit fields...
I don't quite understand the phrase above. Every struct/class in C++
*must* have a destructor. If you don't provide one, the compiler will.
You have to provide a destructor in case you need to clean up the
members of the object being destroyed. If you don't clean up it will
cause a memory leak. In your case, there are no members that are being
dynamically allocated and hence don't need any clean-up to be done in
the destructor. So, the compiler provided destructor will suffice.
Now... The address I'll use is obtained from a vendor function. So
now:

unsigned int addr0;
STATUS stat;
// later
stat = sysPciConfigRead(PCI_CFG_BASE_ADDRESS_0, 4, &addr0);

Now addr0 is 'valid'. The contents at addr0 is really a a register
definition that looks like my_struct above..

I cannot comment much on this since I have no knowledge of the above
vendor library function. But since you've told that the contents at the
obtained address is a register with fields as in your struct, let me
warn you of something. The code might not be portable. Because the
allocation of bits within a byte are unspecified in the standard - This
means the compilers can allocate bits starting from either MSB or LSB.
Therefore it might be more prudent to use appropriate masks and bitwise
operators to extract values from that address.

Srini
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top