Solved

Copying someone's data structure correctly

Posted on 2007-11-23
12
185 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

12 Experts available now in Live!

Get 1:1 Help Now