472,805 Members | 908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 3371

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David Rasmussen | last post by:
If I have this struct S { int arr; }; and I do this: S s1,s2;
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
2
by: Don | last post by:
Using JS, how do I copy an associative array from one frame to another? Here's what I tried, and didn't work. In "main" frame referencing "header" frame: var cookies = new Object(); cookies...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
4
by: Lafer | last post by:
Hello, I am attempting to write a terminal interface program using PowerPC Assembly/C, where I am using an integer-to-ASCII conversion algorithm. Unfortunately, I have run into some difficulties...
12
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is...
5
by: Frederick Gotham | last post by:
I want to copy an array of objects. If it were C, I'd simply do: memcpy( target_array, source_array, sizeof target_array ); However, this won't suffice for objects in C++.
1
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. ...
9
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.