How can I allocate a vector by reference? The issue is that the pointer in the foo function does not return by reference as being allocated.
#include <iostream>
#include <vector>
#include <string>
void foo(std::vector<std::string>*ss) {
if (ss == nullptr) {
ss = new std::vector<std::string>();
}
}
void add(std::vector<std::string> *ss) {
ss->push_back("test");
ss->push_back("test1");
ss->push_back("test2");
ss->push_back("test3");
ss->push_back("test4");
}
int main(void) {
std::vector<std::string> *ss = nullptr;
foo(ss); //Does not get new vector
add(ss); //seg fault because still nullptr
for (auto i : *ss) {
std::cout << i << "\n";
}
ss->clear();
delete ss; ss = nullptr;
return 0;
}