Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
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
ASKER CERTIFIED SOLUTION
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

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
>>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.
Avatar of Infinity08
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'.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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