DJ_AM_Juicebox
asked on
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
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
}
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
Then you cannot do a deep copy.
Maybe you can tell us more about this 3rd party library to try to investigate.
If you are able to see the code for the function, then surely you should be able to see the definition of SomeStruct, no ?
We'd have to see a SomeStruct - there must be a definition of it - although, as I say, it might be 'opaque'.
ASKER
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Thanks
ASKER
ThirdPartyFuncToAllocateIt
I dug into their code (it is some massive opensource library), and it looks like this:
bool ThirdPartyFuncToAllocateIt
{
*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