Pass pointer array by reference - Pointer Problem

Started by
16 comments, last by robert_s 20 years ago
I am trying to pass an empty list of Vec3D to a function that should fill in this array with with some data created in that function. eg. void main() { Vec3D *vList; // unknown for now int vertexCount; // I dont know yet now FillInList( &vList, &vertex_count ) // After the above should know the VertexCount and have the array filled in, so.. // Print the array as final result for(int i=0; i.x ); } the function that Im not happy with is the one below: void FillInList( Vec3D **vList, *vxCount ) { std::list tmpVertices; tmpVertices.push_back( some Vec3D's ); // erase some vectors from list *vxCount = unique_vertices.size(); // Update vertex size vList = new Vec3D*[*vertex_count]; // allocate space for list passed to this func // now load the new analysed data into vList so t can be retrieved std::list::iterator itr = tmpVertices.begin(); for( int i=0 ; itr != tmpVertices.end(); itr++, i++ ) { vList = &*itr; } tmpVertices.clear(); // delete tmp list } Ok the function above works until I delete the tmpVertices which I must delete in the end of the function. Because the array vList is referenced to the tmpVertices list it exists for as long as the tmpVertices list exists. But how can I store this tmpVertices data into Vlist so it can be safely read from main function see the last line in main (for loop). Note: the array is passed empty to FillInList function. It must be allocated there and filled with some data. So I can easily read it from Main teh vList contents. <SPAN CLASS=editedby>[edited by - robert_s on March 30, 2004 7:38:01 AM]</SPAN> </i>
Advertisement
void main(){   Vec3D *vList; // unknown for now   int vertexCount; // I dont know yet now   FillInList( vList, vertex_count )   // After the above should know the VertexCount and have the  array filled in, so..   // Print the array as final result   for(int i=0; i < vList.size(); ++i)     cout<< "Vertex X =" << vList[]<< endl;  // put an 'i' between the brackets, url tags messin it up}the function that Im not happy with is the one below:void FillInList( Vec3D *vList, int &vxCount ){    vxCount = unique_vertices.size(); // Update vertex size   vList = new Vec3D*[*vxCount];   vList.push_back( some Vec3D's );   // erase some vectors from list}    


You don't need the tmpList at all. Since you pass the pointer to vList in, just fill that in the way you would the tempList, and all will be good.


[edited by - stonicus on March 30, 2004 8:11:25 AM]

[edited by - stonicus on March 30, 2004 8:12:08 AM]

[edited by - stonicus on March 30, 2004 8:12:52 AM]
hi,
The problem is that I generate in this function some vectors that are put into intially into tempVertices list. The list I chose is STL as it has several good algorithms that I wish to use. Once the temporaroy list is loaded I shuffle the data bla bla so in the end I take out all the result from STL and put into the vList which I want to return to the main. Please dont ask me why I just need to do it this way. I just dont know how to load data from STL list into the vList array safely so once the function is exited the vList stays filled in with data taht was loaded in teh above function.
thank you.

[edited by - robert_s on March 30, 2004 8:18:39 AM]
Well, if vList is corrupt when you delete the tempList, or hen it goes out of scope, then probably what is happening is you''re copying pointers or address to the vList. Looking at your code, you have ''vList = &*itr''. This is saying set vList.i to be equal to the address of (*itr). So vList just has pointers in it that now point to nothing when tempList goes out of scope.

Yes Stonicus, I know this and I am looking for a way to store these vectors in that function safely so once the function goes out of scope that data is still there.
Hello robert_s,

Hello try follow, it is cleaner to use and you won''t have to create temp list in function calls

// use list if you are going to modified list later fornt of middlevoid FillInList( std::list< Vec3D* >& vList);// use vector if you aren''t realy going to need to modifed// vector can be treated just like an array.void FillInVect( std::Vect< Vec3D* >& vVect);void main(){   std::list< Vec3D* > vList; // unknown for now   std::Vector< Vec3D* > vVect; // unknown for now   FillInList( vList )   FillInVect( vVect )      std::list< Vec3D* >::iterator it=vList.begin();   // get end iterator so loop is faster   std::list< Vec3D* >::iterator it_end=vList.end();   for(;it!=it_end; it++)   {      cout << "Vec3D " << *(*it) << endl;   }   // get size of vect to loop through   int size(vVect.size());   // get pointer to frist data element use so loop is faster   Vec3D** ptr2vect_data = vVect.data();   for(int i(0); i<size; ++i)   {      cout << "Vec3D " << *(ptr2vect_data[ i ]) << endl;   }}// pass in a reference to a list of Vec3D pointersvoid FillInList( std::list< Vec3D* >& vList ){// do what ever you need to do then// push back allocated Vec3D''s you want   vList.push_back( some pointer to Vec3D''s );}// pass in a reference vector of Vec3D pointersvoid FillInVect( std::vector< Vec3D* >& vVect ){// do what ever you need to do then// push back allocated Vec3D''s you want   vVect.push_back( some pointer to Vec3D''s );}


If your using C++ and std use them, pass by refernce (&),
and use vector over list unless your inserting/removing in front or middle.

Lord Bart
How about if you make your array of Vec3D in main() actually a std::vector<Vec3D>? Then you can pass this vector to FillInList as a reference (void FillInList(std::vector&ltVec3D>& VertexList, int& VertexCount)), modify it all you want, and once the function returns, the data is still intact, since the vector was declared within main(). You won't have to handle dynamic memory yourself at all. You're already using the STL, so you might as well use it some more.

[edited by - Agony on March 30, 2004 9:31:24 AM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Sorry guys. I did not exaplin peoperly. I have to use same structure I showed you before. Thie means that in main() there must be a Vec3D *vList array. this Vec3D must be passed to function FillInList by reference as well as int count ie.

main()
{
Vec3D *vList;
// Both are unknown. I know STL is better but this is teh requirement
fillInList( &vList, &count)

// Now I can print out contents of vList below as I did in my first post.

}

Th efunction FillInList does allocation and stores data from a local STL list. I cannot change the structure in anyway, no globals nothing like that. All I have problem is to store from the local STL list (see my first post) into the vList. I am not sure if I dereference pointrs correctly. I just need to correct this line in for()loop:
vList = &*itr;
The code must stay as it is. I mean I cannot change data structures or function parameters.
I'm a bit confused about the nature of vList. In main(), it looks as though it is an array of Vec3D structures. Then you pass the address of the array to FillInList(). This would imply that vList in FillInList is a pointer to an array of Vec3D. However, from within that function, you treat it as an array to pointers of Vec3D, which is entirely different than what it seemed to be at the beginning.

My guess is that from within FillInList, you need to treat vList as a pointer to an array. That way, when you assign things from tmpVertices to vList, you make a copy, not just assign a pointer.
void FillInList( Vec3D **vList, *vxCount ){   std::list tmpVertices;  tmpVertices.push_back( some Vec3D's );  // erase some vectors from list  *vxCount = unique_vertices.size(); // Update vertex size  //!!!! Dereference the pointer vList, to get the array pointer  //!!!! and create a new array of Vec3D, not Vec3D*  *vList = new Vec3D[*vertex_count];  std::list::iterator itr = tmpVertices.begin();  for( int i=0 ; itr != tmpVertices.end(); itr++, i++ )  {    //Dereference the pointer to the array, and then get    //the i-th element.  Copy the data from the iterator.    (*vList)[i] = *itr;  }  tmpVertices.clear(); // delete tmp list } 


[edited by - Agony on March 30, 2004 9:59:46 AM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
I get slightly suspicious when people say that other code cannot be changed. If it improves your design why can't you change it?

The STL containers help you move away from having to worry about things like memory allocation and pointers.

void Fill(std::list<Vec3D>& vList) {//...etc.}std::list<Vec3D> vList;Fill(vList);



[edited by - petewood on March 30, 2004 10:12:18 AM]

This topic is closed to new replies.

Advertisement