 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
toddmarshall2002@yahoo.co Guest
|
Posted: Wed Mar 03, 2004 8:27 pm Post subject: placement new for data structure processing |
|
|
Hi!
I'd like to use placement new to create classes to process data
structures in blocks used in communications; creating a class based on
the structure that is in a block, and using the structure's data as
class data.
However, the problem will be that I can't have any local data in the
class so can't implement any complex algorithms or save states.
(because 'this' from placement new points into the data block)
How should I *best* use classes to process structures in data
communications blocks please any suggestions?
(p.s. i must use preexisting data without redesign)
(p.p.s. I'll also have alignment problems???)
Thanks
todd.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Thu Mar 04, 2004 1:05 pm Post subject: Re: placement new for data structure processing |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | How should I *best* use classes to process structures in data
communications blocks please any suggestions?
|
Define a simple struct to represent the layout of data in
the communications block, and use placement new to point
objects of that type into the block. Define another class
that can operate on the members of the first, and which
holds a pointer to the data block.
eg.,
struct data { char id[4]; char name[8]; char code; };
struct process
{
data *p;
int hash;
process(void *address)
: p(new (address) data)
, hash(p->name[0] + p->name[1])
{ }
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Fri Mar 05, 2004 10:27 am Post subject: Re: placement new for data structure processing |
|
|
Hi,
| Quote: | I'd like to use placement new to create classes to process data
structures in blocks used in communications; creating a class based on
the structure that is in a block, and using the structure's data as
class data.
However, the problem will be that I can't have any local data in the
class so can't implement any complex algorithms or save states.
(because 'this' from placement new points into the data block)
How should I *best* use classes to process structures in data
communications blocks please any suggestions?
|
Short and simple. Don't. As soon as anything of this data is more
than a "byte", you're getting into trouble. C++ does not specify
endianness, whereas your communications layer will. C++ doesn't
specify how bitfields are build-up, or how data will get packed,
structures may have "holes", etc...
You're much safer by using a method that reads the data off the packet
and stores them away.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bill Wade Guest
|
Posted: Fri Mar 05, 2004 10:29 am Post subject: Re: placement new for data structure processing |
|
|
| Quote: | I'd like to use placement new to create classes to process data
structures in blocks used in communications; creating a class based on
the structure that is in a block, and using the structure's data as
class data.
However, the problem will be that I can't have any local data in the
class so can't implement any complex algorithms or save states.
(because 'this' from placement new points into the data block)
How should I *best* use classes to process structures in data
communications blocks please any suggestions?
(p.s. i must use preexisting data without redesign)
(p.p.s. I'll also have alignment problems???)
|
If at all possible use a proxy object that lives someplace else and
holds a pointer to your memory block. Their are some performance
penalties associated with this, but it usually makes things much
easier (lifetime issues may be harder). If necessary you could
maintain a map from the block's address to the proxy's address.
If you really want your class object to have the same address as your
existing memory block, consider the following:
1) It ain't portable. You can probably guess the memory layout of a
class that contains nothing but a bunch of char, or char[] members (or
all int32_t members), but go much beyond that and all bets are off.
2) On any platform I've ever been on the class better not contain
virtual members (or bases). Be extra carefull if you are using
inheritance, especially MI.
3) If the memory block already contains valid data, don't use
placement new. Instead, use reinterpret_cast (probably wrapped up in
a nice-sounding static function name, static MyClass*
MyClassFromMemory(void*)).
4) You said you can't use local data. You didn't say if you needed
local data. If you need it, allocate it on the heap, and keep a map
so you can convert from a block's address to the "local data"
associated with the block. However, if I need to do this, I'm even
more likely to go with the proxy-object solution.
5) If you have alignment issues, be aware that a conforming
implementation could require all class pointers to be aligned (say on
4-byte boundaries), even if the class contains nothing but char.
6) If you write data as one type, and read it as another, the
standard and some implementations require one of those types to be
char (or unsigned char). Of course if you have alignment issues,
you're going to memcpy() raw memory before you treat it as something
more sophisticated (like an int) anyway.
HTH
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Fri Mar 05, 2004 10:31 am Post subject: Re: placement new for data structure processing |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | Hi!
I'd like to use placement new to create classes to process data
structures in blocks used in communications; creating a class based on
the structure that is in a block, and using the structure's data as
class data.
However, the problem will be that I can't have any local data in the
class so can't implement any complex algorithms or save states.
(because 'this' from placement new points into the data block)
How should I *best* use classes to process structures in data
communications blocks please any suggestions?
(p.s. i must use preexisting data without redesign)
(p.p.s. I'll also have alignment problems???)
|
Yes, you're probably onto a loser here due to differing alignment,
byte order and so on. Even if you can define a structure that exactly
matches the file format on your current platform, it probably won't
work if and when you port to another. You should probably implement
one of the following:
- extractors that read individual fields of specified offset,
byte order and length out of a byte array
- classes like Perl's pack and unpack functions (you could use
functions instead, but they would have to have varargs)
- a function that copies bytes in arbitrary order (this
depends on knowing the exact layout of your structure)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Fri Mar 05, 2004 2:39 pm Post subject: Re: placement new for data structure processing |
|
|
Hyman Rosen <hyrosen (AT) mail (DOT) com> wrote
| Quote: | toddmarshall2002 (AT) yahoo (DOT) com wrote:
How should I *best* use classes to process structures in data
communications blocks please any suggestions?
Define a simple struct to represent the layout of data in
the communications block, and use placement new to point
objects of that type into the block. Define another class
that can operate on the members of the first, and which
holds a pointer to the data block.
eg.,
struct data { char id[4]; char name[8]; char code; };
struct process
{
data *p;
int hash;
process(void *address)
: p(new (address) data)
, hash(p->name[0] + p->name[1])
{ }
};
|
What is the advantage of using placement new rather than simply
casting the original pointer?
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|