passing a vector between functions via pointers

42,474

Solution 1

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.

Solution 2

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;
}

Solution 3

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>();
Share:
42,474
shavera
Author by

shavera

Physicist by training. Now doing software development work. Prefer C++, Linux, Qt.

Updated on July 09, 2022

Comments

  • shavera
    shavera almost 2 years

    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;
    
  • Adam Rosenfield
    Adam Rosenfield about 13 years
    C compilers may define NULL as (void*)0, but C compilers ≠ C++ compilers. A conforming C++ compiler must define NULL as "an implementation-defined C++ null pointer constant" (C++03 standard §18.1/4). Since 0 is also a valid null pointer constant, there is no technical reason to prefer either 0 or NULL—the choice is purely stylistic. I personally prefer NULL because it explicitly conveys that you are dealing with a pointer, whereas with 0 it's not always clear from context whether you're dealing with an integer or a pointer.
  • Martin York
    Martin York about 13 years
    I prefer the check for NULL it is visually more distinctive (waiting for nullptr)
  • Dean Burge
    Dean Burge about 13 years
    @AdamRosenfield If 0 is not implementation defined and NULL is, isn't that a technical reason good enough to choose 0 over NULL?
  • Dean Burge
    Dean Burge about 13 years
    But I really didn't mean to start this argument. I said in my answer that I reckon some will disagree. I just respectfully disagree back.