initializing pointer to vector

I've been srhing the forums for ages trying to find an answer and I can't so I figured I'd just ask.

say if you have something like this:

static int manipulateVector( std::vector <std::string>* &vOutVec, std::string sInData ){
// in which a vector is cruelly manipulated
return 1;
}

int main(){
std::vector <std::string> *vHurrah;

manipulateVector( vHurrah );
return 1;
}

How would you go about allocating memory for vHurrah using new?

What I'm trying to do is create some static functions that take an empty vector and other arguments and then add stuff to the vector and return. I think I need to use pass by reference for the changes to be reflected in the calling function (am i right?). I have a feeling someone will suggest I don't use a pointer which is ok but I still wish to know how to do it the way I've shown above just to increase my knowledge.
[978 byte] By [hungry] at [2007-11-18 22:16:53]
# 1 Re: initializing pointer to vector
You're right about one thing, you don't need pointers. Prefer using references instead:

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

static int manipulateVector(vector<string>& vect, const string& data)
{ vect.push_back(data);
return -1;
}

int main()
{ vector<string> vHurrah;

manipulateVector(vHurrah, "Hi");

cout << vHurrah[0] << endl;

system("pause");
}

Why do you need a pointer-to-vector? The vector class handles all the dirty dynamic memory allocations and deallocations so that you don't need to. But if you insists on using a pointer-to-vector to do your work, then you just declare a vector pointer and use new to create the vector object. Then you pass the pointer to the function so the function can manipulate the vector:

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

static int manipulateVector(vector<string>* vect, const string& data)
{ vect->push_back(data);
return -1;
}

int main()
{ vector<string>* vHurrah = new vector<string>;

manipulateVector(vHurrah, "Hi");

cout << (*vHurrah)[0] << endl;

system("pause");
}

Now, which of these 2 options seems easier to work with?

Also, note, that your function declaration:
static int manipulateVector( std::vector <std::string>* &vOutVec, ....
is saying that you have to pass pointer's by reference to the function.
cma at 2007-11-9 0:34:54 >
# 2 Re: initializing pointer to vector
What I'm trying to do is create some static functions that take an empty vector and other arguments and then add stuff to the vector and return.

Declare a vector and passing it by reference to the function. Why even get involved with pointers and dynamic memory? All you're doing is asking for trouble if your program gets larger if you take the approach of allocating memory when none of this needs to be done.
I have a feeling someone will suggest I don't use a pointer which is okIt's not just "ok", it's the correct way (not use pointers).but I still wish to know how to do it the way I've shown above just to increase my knowledge.It is no different than passing a reference to a pointer to an int, double, or a long. Replace "double" with "std::vector<int>" and you get your answer.

#include <vector>

void foo( std::vector<int> *& p )
{
std::vector<int> *temp = new std::vector<int>;
p = temp;
}

int main()
{
std::vector<int> *pInt;
foo( pInt );
// now you have a memory leak if you don't delete!
delete pInt;
}

This is full of problems. First, who is responsible for calling "delete"? Second, how is the caller to know how the memory was allocated? If it were allocated with new[], then the caller must know to use delete[], not just "delete". Third, if you forget to delete, your program now has a memory leak.

This is why others would suggest not to use pointers and dynamic memory. Why torture yourself and invite these problems?

#include <vector>

void foo( std::vector<int>& p )
{
p.push_back(10); // You've now changed the vector
}

int main()
{
std::vector<int> IntV;
foo( IntV );
// IntV has now been changed
}

No memory leaks, no pointers, no dynamic allocation, no problems.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 0:35:57 >