473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

placement new and bit fields.


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

Aug 18 '05 #1
3 1877
> 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

Aug 18 '05 #2
|| 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..

Aug 18 '05 #3
> 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

Aug 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
13
by: Samshayam | last post by:
I have come across the application of placement new in memory mapped i/o in a number of books.I am not able to understand it completely, may be becaues of my lack of knowledge with memory mapped...
1
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy {...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.