?
Solved

Copying someone's data structure correctly

Posted on 2007-11-23
12
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 400 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
Industry Leaders: 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 400 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 400 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 400 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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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