Solved

Copying someone's data structure correctly

Posted on 2007-11-23
12
190 Views
Last Modified: 2010-04-01
Hi,

I'm using some 3d party library which allocates a pointer to one of their structure types. It looks like this:

    int main()
    {
         SomeStruct* p = 0;
         ThirdPartyFuncToAllocateIt(p);
    }

I have no idea what 'SomeStruct' is or how p gets allocated in their function. I want to make a 'deep' copy of it though to pass to a thread, right now I'm just passing the pointer to the thread which im sure will end in distaster at some point. I want to do this:

int main()
{
     while (listenForConnections) {
   
          SomeStruct* p = 0;
          ThirdPartyFuncToAllocateIt(p);
          StartThread(p);
     }
}

void StartThread(SomeStruct* p)
{
    SomeStruct pDeepCopy = new SomeStruct(p); // make a deep copy of it somehow?
}

I hope that's clear - I jsut want to make sure the thread has its own copy to work with safely.

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 100 total points
ID: 20340187
What have you got by way of a description of this struct?  It could well be an 'opaque struct', in which case you've little chance of getting this to work!

Also, surely ThirdPartyFuncToAllocateIt(p); should be

ThirdPartyFuncToAllocateIt(&p);

or

p = ThirdPartyFuncToAllocateIt();
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20340206
yeah sorry, it is:

    ThirdPartyFuncToAllocateIt(&p);

I dug into their code (it is some massive opensource library), and it looks like this:

bool ThirdPartyFuncToAllocateIt(SomeStruct** pp)
{
    *pp = (SomeStruct *) malloc(sizeof(**pp));
    if (*pp == 0) return false;
    bzero((char*)*pp, sizeof(**pp));
    return true;
}

Yeah so that's how it's being allocated internally. What's the right way to copy it then for the thread?

Thank you
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20340212
>>I have no idea what 'SomeStruct' is or how p gets allocated in their function.
Then you cannot do a deep copy.

Maybe you can tell us more about this 3rd party library to try to investigate.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 20340234
If you are able to see the code for the function, then surely you should be able to see the definition of SomeStruct, no ?
0
 
LVL 10

Expert Comment

by:peetm
ID: 20340244
We'd have to see a SomeStruct - there must be a definition of it - although, as I say, it might be 'opaque'.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20340251
Yeah I could, it's just that it's really old and it turns into such a disaster being spread out among multiple files etc etc.

I was more wondering I guess if there is a generic 'safe' way to do it that can apply for all data structures, rather than having to examine the specifics of each one that we come across?

If not, I'll just run that code inside my handler thread then it should be fine, just wondering if there's any easier way,

Thanks
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 20340256
>> I was more wondering I guess if there is a generic 'safe' way to do it that can apply for all data structures

No, because the struct might contain pointers, and to perform a deep copy, you need to know which pointers can be found where in the struct, and what they point to.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 20340262
Also consider that a deep copy is not always possible, depending on how the objects in the applications are related.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 20340265
There cannot be a completely general way as for the ramifications any structs may contain. A good example is IDL (http://msdn2.microsoft.com/en-us/library/aa367062.aspx) that is used to describe data structures for that purpose to autmatically generate code that copes with tasks that you require (even though that might not help you here, but is helpful in understanding the big picture).

That's also the reason why programmers are still required for creating programs ;o)
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 100 total points
ID: 20340309
As peetm has stated, this sounds like it's an opaque pointer. This is a common trick with API. Basically the opaque struct is really just a handle to some object. The point is you as the user of the API are not meant to know the definition of the struct otherwise you might 'tinker' with it. Since you code only ever passes the pointer around and never attempts to dereference it the compiler, within your code, only needs to see the declaration of the struct not the definition.

It is more usual to typedef the pointer so you, as the user, just think you have a handle type.

Example...

//////////////////////////////////////////////////////////////////////////////
// IN API LIBRARY HEADER
// opaque.hpp
struct Opaque;
typedef Opaque * OPAQUE_HANDLE;
//////////////////////////////////////////////////////////////////////////////
// IN API LIBRARY INTERNAL CODE
// opaque.cpp
#include "opaque.hpp"
 
struct Opaque{};
 
void init_sdk(OPAQUE_HANDLE * oh)
{
	*oh = new Opaque;
}
 
void uninit_sdk(OPAQUE_HANDLE oh)
{
	delete oh;
}
//////////////////////////////////////////////////////////////////////////////
// IN YOUR CODE (which only sees API header)
// main.cpp
#include "opaque.hpp"
int main ()
{
	OPAQUE_HANDLE oh;
 
	init_sdk(&oh);
	// Use SDK
	uninit_sdk(oh);
 
	return 0;
}
//////////////////////////////////////////////////////////////////////////////

Open in new window

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20343318
Sorry I realize it was a dumb question. I just stuck the allocated struct in the thread handler so I'm sure to not have to worry about it in this case.

Thanks
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20345556
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question