8
I'm working with a program that makes use of std::vector a lot. Also there are lots of allocations/deallocations taking place, billions of them, and I'm trying to avoid as many of them as possible.
Since I'm relatively new to C++, I have a few questions regarding allocations taking place when using a vector (e.g. when adding elements to it).
I'm on a Win7 64-bit machine, the program is 32-bit and I'm using the current version of MinGW-compilers.
I'd like to know, what happens in the following cases, i.e. if the vector is copied, passed as a reference, ...
1.
std::vector<T> fillVector() {
std::vector<T> returnVector;
...
return returnVector;
}
std::vector<T> myVector = fillVector();
2.
std::vector<T>& fillVector() {
std::vector<T>* returnVector = new std::vector<T>;
...
return (*returnVector);
}
std::vector<T> myVector = fillVector();
3.
std::vector<T>* fillVector() {
std::vector<T>* returnVector = new std::vector<T>;
...
return returnVector;
}
std::vector<T>* myVector = fillVector();
And the following, different operations:
4.
std::vector<T> myVector1;
... (myVector1 being filled)
std::vector<T> myVector = myVector1;
5.
std::vector<T>* myVector1 = new std::vector<T>;
... (myVector1 being filled)
std::vector<T> myVector = (*myVector1);
Assuming I don't want to change the argument in myFunction/changes in to myVector in myFunction wouldn't hurt the rest of the program:
6.
void myFunction(std::vector<T> myParam) {
...
}
std::vector<T> myVector;
... (myVector being filled)
myFunction(myVector);
7.
void myFunction(st