|
||||||
| Forums FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
What you are trying to do is a deep copy of a structure. In that case you have to provide some storage that c points to. This is not a C++ issue BTW, you would have the same issue in C.
|
|
||||
|
Thank you.
How can I provide that storage? I though it was a C++ memory management issue because I did something similar in C but with less complexity and correct results. I don't understand why it stop pointing to the right place when I did the casting. I tried to do something like: Code:
to->Table = (test2 *)((char)destination + 8); The code is wrong but at least I get to->Table points to destination + 8, inside the shared memory. I still looking for the answer. |
|
|||
|
You have to allocate some space for it somewhere in shared storage. What you are trying to copy is not an object that is all of one piece in memory. It contains a pointer that points to another piece. Therefore your destination object must also be two pieces, one for the base object and one for the auxiliary structure pointed to from the base object.
What C++ could do for you is encapsulate the copying in a deep-copy method. Also if you find yourself doing casting in C++, most of the time you are doing something wrong. |
|
||||
|
Thank you ken yap. Now I get the point.
Quote:
Thanks again. |
|
|||
|
It means that most of the time, casting is not needed in C++. The typing rules should handle most of the type coercions automatically, or the coercions should coded in a few places. If you find yourself needing a cast, perhaps something is wrong with the code.
Also if an arbitrary area of memory is declared as void *, this is automatically compatible with any typed pointer, and you don't need a cast. (Provided alignment constraints are adhered to.) malloc() returns a void * that is compatible with all typed pointers. Of course, you really should be using new() instead of malloc(). |
|
||||
|
Finally.
I think I should write my own shared memory heap-like manager. Doing the following I made it work properly. Code:
SharedMemory *sm1 = new SharedMemory(SHAMEM_ID, 1600, true); void *pool = sm1->Get(); test *tp = (test *)pool; tp->c = (test2 *)(new SharedMemory ( SHAMEM_ID + 10, sizeof(test2) * 10, true ))->Get(); test *t = new test; t->a = 123; t->b = 234; t->c = (test2 *) malloc(sizeof(test2) * 10); t->c[0].a = 777; t->c[7].a = 888; Clone(t, pool); |
|
||||
|
Yes, you're right. I did the cast because I get a compilation error without it.
The following results in: invalid conversion from ‘void*’ to ‘test*’ Code:
test *to = destination; Originally Clone() was not function, it was a method that uses new operator to dynamically allocate memory. I also tried overloading new() and returning the start of the shared memory. It wasn't work either. It will be crazy to write a function that updates the pointer in every method that manipulates the object so I voted to a simply structure that imitates straightforward memory allocation but as you saw it wasn't that simple (at least for me). |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|