Archived

This topic is now archived and is closed to further replies.

Pass pointer array by reference - Pointer Problem

This topic is 5004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
  • Share this post


    Link to post
    Share on other sites

    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]

    Share this post


    Link to post
    Share on other sites
    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]

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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 middle

    void 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 pointers

    void 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 pointers

    void 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

    Share this post


    Link to post
    Share on other sites
    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<Vec3D>& 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]

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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]

    Share this post


    Link to post
    Share on other sites
    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]

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster

    /*
    I had to perform some reconstructive surgery after the copy and paste.
    Next time post code in source tags.
    */


    void main()
    {
    Vec3D *vList;
    unsigned int vertexCount;

    FillInList( &vList, &vertex_count );

    // Don''t forget to clean up vList when you''re done with it.

    for(unsigned int i=0; i < vertexCount; ++1)
    {
    cout << "Vertex X =" << vList[i].x << " Y = " << vList[i].y /* etc */ << endl;
    delete vList[i];
    }

    delete [] vList;

    }



    void FillInList( Vec3D ** vList, unsigned int * vxCount )
    {
    std::list<Vec3D> tmpVertices;

    /*
    Do stuff with tmpVerticies
    */


    *vxCount = tmpVerticies.size();
    (*vList) = new Vec3D*[*vxCount];

    std::list<Vec3D>::iterator itr = tmpVertices.begin();

    // std::copy would be usefull here, but I don''t feel like doing the grunt work.

    for(int i = 0; itr != tmpVertices.end(); ++itr, ++i )
    {
    // We want vList to contain copies of what''s in the list.

    // Vec3D better have a copy constructor!

    (*vList)[i] = new Vec3D(*itr);
    }

    // Redundant - tmpVerticies (and it''s contents) will be destroyed when the function exits.

    //tmpVertices.clear();

    }

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    Oops. Misplaced *


    /*
    I had to perform some reconstructive surgery after the copy and paste.
    Next time post code in source tags.
    */


    void main()
    {
    Vec3D *vList;
    unsigned int vertexCount;

    FillInList( &vList, &vertex_count );

    // Don''t forget to clean up vList when you''re done with it.

    for(unsigned int i=0; i < vertexCount; ++1)
    {
    cout << "Vertex X =" << vList[i].x << " Y = " << vList[i].y /* etc */ << endl;
    delete vList[i];
    }

    delete [] vList;

    }



    void FillInList( Vec3D ** vList, unsigned int * vxCount )
    {
    std::list<Vec3D> tmpVertices;

    /*
    Do stuff with tmpVerticies
    */


    *vxCount = tmpVerticies.size();
    (*vList) = new Vec3D[*vxCount];

    std::list<Vec3D>::iterator itr = tmpVertices.begin();

    // std::copy would be usefull here, but I don''t feel like doing the grunt work.

    for(int i = 0; itr != tmpVertices.end(); ++itr, ++i )
    {
    // We want vList to contain copies of what''s in the list.

    // Vec3D better have a copy constructor!

    (*vList)[i] = new Vec3D(*itr);
    }

    // Redundant - tmpVerticies (and it''s contents) will be destroyed when the function exits.

    //tmpVertices.clear();

    }

    Share this post


    Link to post
    Share on other sites
    Here is a utility function for you. Should make things easier for you.


    void makeVec3DArrayFromVec3DList(Vec3D*& array, int& size, const std::list<Vec3D>& cont) {
    Vec3D* newArray = new Vec3D[cont.size()];
    std::copy(cont.begin(), cont.end(), newArray);
    array = newArray;
    size = cont.size();
    }


    This could easily be templatised.

    [edited by - petewood on March 30, 2004 10:28:23 AM]

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    Petewood;

    The array is an array of pointers. That is why I didn''t use std::copy.

    Incidentally, the line where you print out the contests in my source should be vList->x not vList[i].x

    Share this post


    Link to post
    Share on other sites
    Thanks Anonymous Poster. this is exactly what I needed.
    I know fo rsome doesnt make sense but this is what I wanted to achieve. Little correction

    (*itr) = new V3(*itr); <<<--- this doesnt compile on my machine
    I used this instead
    *vList = new V3[*vertex_count];

    Thank you all!

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    I don''t know where you got that line of code; it isn''t anywhere in the stuff I posted.

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster

    My code is horribly broken anyway.

    Replace (*vList)[i] = new Vec3D(*itr);
    with (*vList)[i] = *itr;

    and remove delete vList[i];

    Share this post


    Link to post
    Share on other sites