copying an object to a char array and back

R

Rahul

Hi,

I have following two classes
class base{
int i;
public:
virtual void fun() { cout<<i<<"Base \n"; }
};

class d: public base{
int j;
public:
void fun() { cout<<"Derived \n"; }
};

and the code is doing something like this.
d d1;
char c[100];
bcopy((char *)&d1, c, sizeof(d));
factory->process(c); // some factory member which accepts a char *

// Inside factory->process ()
d *dp=reinterpret_cast<d *> (c);
dp->fun();

Question: Is it ok to copy an object to a char array and cast it back
like this. Can there be any memory alignment problem (on a single
processor only). Or any other problem.
 
V

Vikram

Rahul said:
Question: Is it ok to copy an object to a char array and cast it back
like this. Can there be any memory alignment problem (on a single
processor only). Or any other problem.

In general, no. Just copying it into a char array will not "create" an
object. As in, it will not invoke its constructor. So a memory image of
an object is not actually a replacement for it.
 
S

sonison.james

Rahul said:
Question: Is it ok to copy an object to a char array and cast it back
like this. Can there be any memory alignment problem (on a single
processor only). Or any other problem.

No, this is not the way to serialize and deserialize objects. One
obvious problem is shallow copy problem (if class has member pointers)
with this approach.

Thanks and regards
Sonison James
 
R

Rahul

Vikram said:
In general, no. Just copying it into a char array will not "create" an
object. As in, it will not invoke its constructor. So a memory image of
an object is not actually a replacement for it.

I know that its not equivalant to an object, but my question was
Can this kind of hack to pass an object through a char array creat some
core dump (crash) or not.
My guess is NO because as long as we are on the same processor, the
object memory layout will never change so it should work always. But
its never a good coding practice.
Is there any other situation.
 
P

peter koch

Rahul said:
Hi,

I have following two classes
class base{
int i;
public:
virtual void fun() { cout<<i<<"Base \n"; }
};

class d: public base{
int j;
public:
void fun() { cout<<"Derived \n"; }
};

and the code is doing something like this.
d d1;
char c[100];
bcopy((char *)&d1, c, sizeof(d));
factory->process(c); // some factory member which accepts a char *

// Inside factory->process ()
d *dp=reinterpret_cast<d *> (c);
dp->fun();

Question: Is it ok to copy an object to a char array and cast it back
like this.
No it is not. In practice this can give problems even for very simple
classes that manage pointers to internal storage (std::string could do
so).
Can there be any memory alignment problem (on a single
processor only).
Also that. There is no guarantee that c is aligned at all.
Or any other problem.

Why do yo do so in the first place? Why not simply create a new object
or copy the "normal" way? (d d2(d1) in the code above).

/Peter
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top