470,849 Members | 1,100 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

overlay class over memory

I am relatively new to C++ so this is probably a basic question: Can I
overlay a class over a chuck of memory just like one can overlay a
struct over memory. I need to parse a frame comming from the network
hardware.

Thanks,

Piotr

Feb 26 '06 #1
5 3201
lr******@ftml.net wrote:
I am relatively new to C++ so this is probably a basic question: Can I
overlay a class over a chuck of memory just like one can overlay a
struct over memory. I need to parse a frame comming from the network
hardware.


You should take a look at "placement new".

V
--
Please remove capital As from my address when replying by mail
Feb 26 '06 #2

<lr******@ftml.net> schrieb im Newsbeitrag
news:11**********************@u72g2000cwu.googlegr oups.com...
I am relatively new to C++ so this is probably a basic question: Can I
overlay a class over a chuck of memory just like one can overlay a
struct over memory. I need to parse a frame comming from the network
hardware.


Only if your class looks like a classic struct. If you add "hidden"
varialbles to the class, e.g. a a pointer to the virtual function table do
to a virtual function in the class, ... it will mess up the memory
alignment.
Feb 26 '06 #3
Is placement new fixing the problem of virtual tables?

Piotr

Feb 26 '06 #4
lr******@ftml.net wrote:
Is placement new fixing the problem of virtual tables?


What book are you reading that doesn't explain 'new'? "Placement
new" is not different at all in the procedure of constructing the
object. The only difference is actual memory allocation, the
"regular" 'new' allocates, the "placement" doesn't.

V
--
Please remove capital As from my address when replying by mail
Feb 26 '06 #5
lr******@ftml.net wrote:
I am relatively new to C++ so this is probably a basic question: Can I
overlay a class over a chuck of memory just like one can overlay a
struct over memory. I need to parse a frame comming from the network
hardware.


Simply casting the memory *might* work except for alignment and byte order.

This is a posting I did a while ago - might help.

http://groups.google.com/group/comp....e4a6bda?&hl=en
Feb 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Never Best | last post: by
1 post views Thread by sendhil | last post: by
reply views Thread by odie5533 | last post: by
3 posts views Thread by kaczmar2 | last post: by
9 posts views Thread by Chuck Anderson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.