Stack vs Heap allocation?

Hi
I was wondering whether it is better to store something on the stack vs on the heap?

Consider the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct config
{
	int data = 43;
};

class Manager
{
	private:
		std::vector<config> configs; //on stack like this?
		std::vector<std::unique_ptr<config>> configs; //or on heap like this?
	public:
		config& GetConfig(int index);
};


As you can see Manager has a vector of configs structs, and Manager is only ever going to be giving out references to the configs.
Would it be better to use pointers and allocate the structs on the heap?
or on the stack?
Last edited on
That is a red herring.
1
2
3
4
5
#include <vector>

int main() {
 std::vector<int> foo { 7, 42, 66 };
}

Where the foo is?
Where are the integers 7, 42, and 66?

Lets make that more interesting:
1
2
3
4
5
#include <vector>

int main() {
 std::vector<std::vector<int>> bar( 1, { 7, 42, 66 } );
}

What has changed?


The stack is a limited resource, but the std::vector stores its elements in the Free Store, not in stack. Hence the herring.
Topic archived. No new replies allowed.