overlay class over memory

L

lromanus

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
 
V

Victor Bazarov

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
 
F

Frank Schmidt

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.
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top