473,227 Members | 1,652 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,227 software developers and data experts.

placement new versus .. more


I've got a struct which is comprised of POD types. I know in advance
where in memory the struct resides. To make a long story short, I'm
doing transfers of 16 bit ADC sampled data between 'cards'. At the
front end I configure the memory such that - say card 1 will transfer
data to card 2 at a specified region. A header is accompanied with
each data transfer. So now lets say card 1 transferred the header to
location 0xD0000000 of card2. Assume the header is akin to:

typedef unsigned int uint_type;
struct temp {
uint_type param1;
uint_type param2;
uint_type param3;
// etc
};
To see what's in temp, I do:
temp *ptr_temp = (temp*)0xD0000000; // I already know where to map to.
// now do something with the junk received.
int const p1 = ptr_temp->param1;

As far as I know the approach above is perfectly legal or is this a
candidate for placement new?

Here's my confusion. I'm reading Clines text (yes hard copy of the
FAQ) and I'm tryign to get my head around placement new but I'm not
sure if that buys me anything here.

As always, thanks in advance.

Feb 11 '06 #1
5 1489
ma740988 wrote:

I've got a struct which is comprised of POD types. I know in advance
where in memory the struct resides. To make a long story short, I'm
doing transfers of 16 bit ADC sampled data between 'cards'. At the
front end I configure the memory such that - say card 1 will transfer
data to card 2 at a specified region. A header is accompanied with
each data transfer. So now lets say card 1 transferred the header to
location 0xD0000000 of card2. Assume the header is akin to:

typedef unsigned int uint_type;
struct temp {
uint_type param1;
uint_type param2;
uint_type param3;
// etc
};
To see what's in temp, I do:
temp *ptr_temp = (temp*)0xD0000000; // I already know where to map to.
// now do something with the junk received.
int const p1 = ptr_temp->param1;

As far as I know the approach above is perfectly legal or is this a
candidate for placement new?


Placement new will construct an object for you at a memory location that you
specify by means of the passed pointer. The line above does *not* construct
an object at location 0xD0000000. It just makes ptr_temp point there.

If I understand you correctly, card1 has already put all the data there that
you would expect in the header. In that case, the line above is *right* and
placement new would be *wrong* because the object has already been created
by card1.
Best

Kai-Uwe Bux
Feb 11 '06 #2
* ma740988:
I've got a struct which is comprised of POD types. I know in advance
where in memory the struct resides. To make a long story short, I'm
doing transfers of 16 bit ADC sampled data between 'cards'. At the
front end I configure the memory such that - say card 1 will transfer
data to card 2 at a specified region. A header is accompanied with
each data transfer. So now lets say card 1 transferred the header to
location 0xD0000000 of card2. Assume the header is akin to:

typedef unsigned int uint_type;
struct temp {
uint_type param1;
uint_type param2;
uint_type param3;
// etc
};
To see what's in temp, I do:
temp *ptr_temp = (temp*)0xD0000000; // I already know where to map to.
// now do something with the junk received.
int const p1 = ptr_temp->param1;

As far as I know the approach above is perfectly legal
It's compiler-specific, and moreover, since judging from what you write
it's likely that that region resides on the card itself (otherwise why
use an absolute address?), it's also hardware-specific.
or is this a candidate for placement new?
No.

Here's my confusion. I'm reading Clines text (yes hard copy of the
FAQ) and I'm tryign to get my head around placement new but I'm not
sure if that buys me anything here.


No, it doesn't. Placement new constructs an object in already existing
storage. If you had a non-POD you wished to have in card memory you'd
have to use placement new, but not with a POD.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 11 '06 #3
Kai-Uwe, Alf. Got it. Thanks!!!

Feb 11 '06 #4

"Alf P. Steinbach" <al***@start.no> skrev i meddelandet
news:45************@individual.net...
* ma740988:
I've got a struct which is comprised of POD types. I know in
advance
where in memory the struct resides. To make a long story short,
I'm
doing transfers of 16 bit ADC sampled data between 'cards'. At
the
front end I configure the memory such that - say card 1 will
transfer
data to card 2 at a specified region. A header is accompanied
with
each data transfer. So now lets say card 1 transferred the header
to
location 0xD0000000 of card2. Assume the header is akin to:

typedef unsigned int uint_type;
struct temp {
uint_type param1;
uint_type param2;
uint_type param3;
// etc
};
To see what's in temp, I do:
temp *ptr_temp = (temp*)0xD0000000; // I already know where to map
to.
// now do something with the junk received.
int const p1 = ptr_temp->param1;

As far as I know the approach above is perfectly legal


It's compiler-specific, and moreover, since judging from what you
write it's likely that that region resides on the card itself
(otherwise why use an absolute address?), it's also
hardware-specific.


And it is also OS specific. :-)

The addresses used in the program most likely is in a virtual address
space. The card probably works with a physical memory address.

Are we sure that these are connected ?
Bo Persson
Feb 12 '06 #5
> >> As far as I know the approach above is perfectly legal

It's compiler-specific, and moreover, since judging from what you
write it's likely that that region resides on the card itself
(otherwise why use an absolute address?), it's also
hardware-specific.


And it is also OS specific. :-)


Not sure I'm followinig you here.

I disovered what my problem was. Initially I had a _true_ POD header:
struct header {
unsigned int data_size;
unsigned int message_id;
// more stuff
};

Then I changed it to:
struct header {
unsigned int data_size;
unsigned int message_id;
header()
: data_size(0)
, message_id(0)
{}
};

For the latter, header is no longer a POD. As a result all bets are
off, hence I suspect thats the reason the 'destination' was seeing a
bogus value for message_id. It's as if it wasn't initialized.

So I'll just resort back to the C style struct (the former) and memset
the damn thing. This alleviates the need to have to serialize the data
and all the other mess that accompanies the non pod struct.

Feb 13 '06 #6

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>
8
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some...
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 }
5
by: Marty | last post by:
I am wondering what is the difference in the placement between the 2 uses of the indirection operator? Or is there a difference? AcDbBlockTable *pBlockTable = NULL; AcDbDatabase* pDB =...
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.