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! ]
|
|
0
|
|
|
|
Reply
|
toddmarshall2002
|
3/3/2004 8:27:01 PM |
|
toddmarshall2002@yahoo.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])
{ }
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Hyman
|
3/4/2004 1:05:04 PM
|
|
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?
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! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
3/5/2004 10:27:01 AM
|
|
> 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! ]
|
|
0
|
|
|
|
Reply
|
wade
|
3/5/2004 10:29:31 AM
|
|
toddmarshall2002@yahoo.com wrote:
> 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! ]
|
|
0
|
|
|
|
Reply
|
Ben
|
3/5/2004 10:31:30 AM
|
|
Hyman Rosen <hyrosen@mail.com> wrote in message
news:<1078348675.462360@master.nyc.kbcfp.com>...
> toddmarshall2002@yahoo.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! ]
|
|
0
|
|
|
|
Reply
|
Nicola
|
3/5/2004 2:39:56 PM
|
|
|
5 Replies
104 Views
(page loaded in 0.084 seconds)
Similiar Articles: transaction vs event - comp.lang.vhdl... for example, the not-very-pretty traditional form of clocked process ... transaction, and you would like to detect when someone puts a new value on that data structure ... Data Structure Using Proc Transpose - comp.soft-sys.sas... steps > that create dynamic data structures ... Learned new things today .. Awesome ... look at the solution posted by Data _null_ using Proc Transpose and BY processing in ... Strong motion data - comp.soft-sys.matlabHello I am new to matlab. I am processing the strong motion data(earthquake record) from ... object in uipanel4 % eventdata structure with the ... Strong motion data - comp ... Agena - a new procedural programming language - comp.unix.solaris ...Hello, I would like to introduce you to a new language called ... provides fast real and complex arithmetics, efficient text processing, flexible data structures ... VHDL Design for running sorter - comp.arch.fpgaSo, in every clock cycle, I get a new data ... need to have the 24th-position data available to me for further processing. ... bubble sort in structure - comp.lang.c VHDL ... How can I combine incrementing variable names? - comp.soft-sys ...NEW_2000.MAT This would be all fine and dandy to use for data processing in Matlab, however, the genius who wrote ... First read the data into a structure ... Structure to matrix - comp.soft-sys.matlab... us :) 1) I want to process the numeric data but ... and Reshaping Matrices :: Data Structures (MATLAB ..... is most useful when you want to expand a matrix by adding new ... Odd BACKUP error: unsupported file structure ! - comp.os.vms ...... ll see i backup can then process the disk faster. If so, it would point to data structure ... file with a brand spanking new (odd expression that is!) RMS indexed structure ... why in hell the uigetdir works so slowly? - comp.soft-sys.matlab ...I use Matlab R2009a to do some image processing, since ... But my computer is new. Pentium(R) Dual-core CPU ... pay off, hell ... performance with a different data structure ... live video handles is run in other axes when doing process of ...... Thanks for your response, i have make 2 new axes, and ... live video handles is run in other axes when doing process of ... % handles structure with handles and user data ... NYPPEX- Private Equity Secondary Market Liquidity SolutionsSTRUCTURES. Single Interests; QMS; Strips; Structured Transactions ... MARKET DATA. NYPPEX Market Data is the authoritative ... Clients benefit from the ability to meet new ... C++ Tutorials - C++ File ProcessingFile processing in C++ is performed using the fstream class. Unlike the FILE structure, fstream is a complete C++ ... If FileName is a new file, data is written to ... 7/14/2012 10:21:27 PM
|