Solved

Copying someone's data structure correctly

Posted on 2007-11-23
12
186 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now