Copying someone's data structure correctly

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
DJ_AM_JuiceboxAsked:
Who is Participating?
 
peetmConnect With a Mentor Commented:
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
 
DJ_AM_JuiceboxAuthor Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
>>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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Infinity08Commented:
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
 
peetmCommented:
We'd have to see a SomeStruct - there must be a definition of it - although, as I say, it might be 'opaque'.
0
 
DJ_AM_JuiceboxAuthor Commented:
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
 
Infinity08Connect With a Mentor Commented:
>> 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
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
Also consider that a deep copy is not always possible, depending on how the objects in the applications are related.
0
 
jkrConnect With a Mentor Commented:
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
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
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
 
DJ_AM_JuiceboxAuthor Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
0
All Courses

From novice to tech pro — start learning today.