468,458 Members | 1,783 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

copying an object to a char array and back

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
4 2988

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

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

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.

Similar topics

10 posts views Thread by David Rasmussen | last post: by
21 posts views Thread by Matteo Settenvini | last post: by
5 posts views Thread by Roy Hills | last post: by
4 posts views Thread by Lafer | last post: by
12 posts views Thread by anonymous | last post: by
5 posts views Thread by Frederick Gotham | last post: by
1 post views Thread by RonLandreth | last post: by
9 posts views Thread by Mr John FO Evans | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.