memcpy() question in C++

E

Eric

Tell me why the following doesn't work...

void MyClass::StoreCommand( struct command cmdToStore )
{
memcpy( (char*)&theCommand,
(char*)&cmdToStore,
sizeof( struct command ) );
}

theCommand is also a struct command type.

This just makes a byte-by-byte copy of the origin (cmdToStore) to the
destination (theCommand), yet the destination ends up all corrupted
and bearing no resemblance to the origin.
 
R

Roland Pibinger

Tell me why the following doesn't work...

void MyClass::StoreCommand( struct command cmdToStore )
{
memcpy( (char*)&theCommand,
(char*)&cmdToStore,
sizeof( struct command ) );
}

theCommand is also a struct command type.

This just makes a byte-by-byte copy of the origin (cmdToStore) to the
destination (theCommand), yet the destination ends up all corrupted
and bearing no resemblance to the origin.

Since operator= by default also just does a byte-by-byte copy you may
try:

void MyClass::StoreCommand( struct command cmdToStore )
{
theCommand = cmdToStore;
}

Best wishes,
Roland Pibinger
 
P

Phlip

Eric said:
void MyClass::StoreCommand( struct command cmdToStore )
{
memcpy( (char*)&theCommand,
(char*)&cmdToStore,
sizeof( struct command ) );
}

theCommand is also a struct command type.

That kind of memcpy() only works if theCommand is a Plain Ol' Data
Structure. Look that up for the rules; it's essentially the same as what
you could write command aCommand = {0} for.

C++ generally should not use memcpy(). Try theCommand = cmdToStore.

However, you could have an unrelated problem. (Mistakes with memcpy often
_resemble_ healthy code; the bytes should not be all screwed up.) Make sure
that cmdToStore was healthy, that its copy constructor calls correctly when
you pass it to StoreCommand, and that theCommand has a valid storage place.

This might fix everything:

void MyClass::StoreCommand( struct command & cmdToStore )

Report back which tip worked!
 
H

Heinz Ozwirk

Eric said:
Tell me why the following doesn't work...

void MyClass::StoreCommand( struct command cmdToStore )
{
memcpy( (char*)&theCommand,
(char*)&cmdToStore,
sizeof( struct command ) );
}

theCommand is also a struct command type.

This just makes a byte-by-byte copy of the origin (cmdToStore) to the
destination (theCommand), yet the destination ends up all corrupted
and bearing no resemblance to the origin.

Usually you don't want a byte-to-byte copy of a struct or class. Such user defined type may have pointers that must not be copied, and they hopefully have assignment operators that know how to handle the internals of a class|struct. Simply use assignment and leave it to the compiler to decide whether to call a user defined assignment operator or simply call memcpy (or something similiar.

The easiest way to avoid errors with memcpy is to forget that memcpy exists. Each struct and class has a copy constructor and an assignment operator, so you can simply replace your memcpy with

theCommand = cmdToStore;

That is much easier to write and understand than memcpy and the only thing more difficult is making errors.

HTH
Heinz
 
G

gangs

:) where have you declared struct theCommand? and where are you trying
to access it?

gangs.
 
R

Ron Natalie

Roland said:
Since operator= by default also just does a byte-by-byte copy you may
try:
Actually operator= does a subobject-by-subobject copy which
is more likely to be correct.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top