Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a vector between functions via pointers

It was suggested to me to use pointers to add a vector that I wanted to pass from some existing function to another function. I am really stuck on how to get the information back out of that pointer though. I've tried a number of things I've read here and there so let me demonstrate what I'm talking about.

primary program:

std::vector<float> * dvertex=NULL;

track.calculate(irrelevant stuff, dvertex)

secondary program (track, calculate)

track::caclulate(irrelevant stuff, vector<float> * dvertex)
{
...
vector<float> pos;
... pos filled after some calculations
if(! (dvertex==NULL))
{
  dvertex = &pos1;
}

back to primary, unless I messed up something above, here's some things I've tried

1

(*dvertex).at(0)
float z = (*dvertex).at(0)

2

(*dvertex)[0]

and a bunch of stuff that just plain didn't compile. I'm quite stuck as I'm not sure how to get the specific values out of that vector in the main program. I even thought it might be the if(! (dvertex==NULL)) bit, so I changed it to if(dvertex==NULL) but still no joy. Any help would be greatly appreciated.

*Edit/Update*Thanks so much everyone for the help, but I fear I'm still doing it wrong.

So following the suggestions that I just pass a reference: I did this:

primary

std::vector<float> dvertex;
track.calculate( foo, &dvertex);

secondary stayed the same (with !Null check)

primary

std::cout<<dvertex[0]<<std:endl; 

(among other attempts to actually use the data)

Thanks a lot for any thoughts on what I'm still doing improperly. Everything compiles, the program just freezes when it gets to a point that the data from dvertex is used.

Edit:Final fix

in the secondary program I needed

*dvertex = pos1;

instead of

dvertex = &pos1;
like image 472
shavera Avatar asked Mar 18 '11 04:03

shavera


3 Answers

I'm not sure why these didn't compile for you, because they're valid as long as the pointer is valid and not null:

void f(std::vector<int>* v)
{
    if( v != 0 ) {
        int n = (*v)[0];     // ok
        int m = (*v).at(0);  // ok
        int o = v->at(0);    // ok
    }
}

But never mind that. Use a reference if you must change the vector, and a const reference if you must not. There's rarely if ever a need to take a container by pointer.

Also, I suggest you check pointers against 0, not NULL, because sometimes NULL is defined as (void*)0 as per C compilers. But some people may argue otherwise here.

like image 76
wilhelmtell Avatar answered Oct 20 '22 03:10

wilhelmtell


If you're going to modify the vector, you probably just want to pass it by reference. If you do use a pointer, however, you need to define a vector in main, and then pass the address of that vector:

void calculate(std::vector<float> *vertex_array) { 
    vertex_array->pushback(1.0f);
    vertex_array->pushback(2.0f);
}

int main() {    
    std::vector<float> vertexes;
    calculate(&vertexes);

    std::copy(vertexes.begin(), vertexes.end(), 
        std::ostream_iterator<float>(std::cout, "\n"));
    return 0;
}
like image 40
Jerry Coffin Avatar answered Oct 20 '22 03:10

Jerry Coffin


See my note above, but for your scenario to work, you need

std::vector<float> * dvertex=NULL;

to be

std::vector<float> * dvertex = new std::vector<float>();
like image 36
jonsca Avatar answered Oct 20 '22 03:10

jonsca