Sign in to follow this  
guyaton

need pointers on pointers

Recommended Posts

I'm trying to create an array by passing a pointer into a function and then having the function create the pointer with the new command. I tried a few combinations to get this to work. One of them kind of works where it only creates the first item on the list and not the rest of the array. Here is some syntax that might better explain it. typedef struct SQuadNodes { //... SQuadNodes(); ~SQuadNodes(); } SQuadNodes, *LPSQUADNODES; HRESULT CountPolygonsContained( /*some variables here*/, LPSPOLYGONGROUP *pOutPolygonsContained = NULL ) { //code here... *pOutPolygonsContained = new SPolygonGroup[pOutPolygonCount](); memcpy( pOutPolygonsContained, tempPolygonGroup, pOutPolygonCount * sizeof(SPolygonGroup) ); //...code here } what should i change so that it will create an array of the desired size?

Share this post


Link to post
Share on other sites
You are almost there. What you would need to do is pass a pointer to a pointer... it might look something like this:


typedef struct SQuadNodes
{
//...
SQuadNodes();
~SQuadNodes();
} SQuadNodes, *LPSQUADNODES;

HRESULT CountPolygonsContained( /*some variables here*/, LPSPOLYGONGROUP **pOutPolygonsContained = NULL )
{
//code here...
*pOutPolygonsContained = new SPolygonGroup[pOutPolygonCount];
memcpy( *pOutPolygonsContained, tempPolygonGroup, pOutPolygonCount * sizeof(SPolygonGroup) );
//...code here
}



and then the code that calls CountPolygonsContained would have to be slightly different too.

Share this post


Link to post
Share on other sites
this will work. you need to pass what's called a handle (a pointer to a pointer) since you are trying to set the value of the pointer itself. whenever you pass anything to a function you are always passing a _copy_ of that something. so we use pointers when we want to modify what the pointer points to, but the actual pointer you are passing is still a copy. it's just that it's a copy of a memory address so it still points to what you want to modify that makes that scenario work. in your case you actually want to modify the value of the pointer itself (i.e. change what it points to) so in that case, you need a pointer to the pointer. :)


HRESULT CountPolygonsContained( /*some variables here*/, LPSPOLYGONGROUP **pOutPolygonsContained = NULL )
{
//code here...
*pOutPolygonsContained = new SPolygonGroup[pOutPolygonCount]();
memcpy( pOutPolygonsContained, tempPolygonGroup, pOutPolygonCount * sizeof(SPolygonGroup) );
//...code here
}

Share this post


Link to post
Share on other sites
ah, then shouldn't this line:

memcpy( pOutPolygonsContained, tempPolygonGroup, pOutPolygonCount * sizeof(SPolygonGroup) );



be:

memcpy( *pOutPolygonsContained, tempPolygonGroup, pOutPolygonCount * sizeof(SPolygonGroup) );



-me

Share this post


Link to post
Share on other sites
The only problem is that the memcpy line doesn't work because the array isn't properly created. When it is created only the zeroith column is created. the 1st column is an invalid pointer. which is the confusing part to me.

so i'm guessing it is either something wrong with this line, or the way i'm passing it?


*pOutPolygonsContained = new SPolygonGroup[*pOutPolygonCount]();

Share this post


Link to post
Share on other sites
typedef'ing structures is totally unnesscary in C++, raw arrays are painful use standard library containers probably deque/vector will do. Pass parameters that need to modify the variable passed to the function by reference instead of pointers. With all this in mind the prefered code in c++ will be:


#include <vector>
#include <algorithm>

class poly_group;

HRESULT CountPolygonsContained( /*some variables here*/, std::vector<poly_group>& outPolygonsContained) {
std::vector<poly_group> tempPolygonGroup;
//la-la-la
std::copy(tempPolygonGroup.begin(), tempPolygonGroup.end(), outPolygonsContained.begin());
//la-la-la
}





Last hungarian notation unnesscary today & ugly (just a personal perference don't take it to heart ;-) ).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this