need pointers on pointers

Started by
5 comments, last by snk_kid 19 years, 8 months ago
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?
~guyaton
Advertisement
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.
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}
What I am passing is already a pointer to a pointer tho according to the definition of the structure right? Adding another pointer unfortunatly didn't resolve anything.
~guyaton
ah, then shouldn't this line:
 memcpy( pOutPolygonsContained, tempPolygonGroup, pOutPolygonCount * sizeof(SPolygonGroup) );


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


-me
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]();
~guyaton
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 ;-) ).

This topic is closed to new replies.

Advertisement