By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

copying an object to a char array and back

P: n/a
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.

Jul 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Rahul wrote:
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.

Jul 11 '06 #2

P: n/a
Rahul wrote:
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

Jul 11 '06 #3

P: n/a

Vikram wrote:
Rahul wrote:
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.
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.

Jul 11 '06 #4

P: n/a

Rahul wrote:
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

Jul 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.