vector of queue segmentation fault

I don't understand why I get a segmentation fault in this program, where is my mistake ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <iostream>
#include <queue>
#include <vector>

#define VECNUM 10
#define QUEUENUM 15

using namespace std;

int main ()
{
  vector< queue<int> > VectorQueue;
  VectorQueue.reserve(VECNUM);
  
  for (int i = 0; i<VECNUM; i++)
  {
	for (int j = 0; j<QUEUENUM; j++)
	{
		cout<<"Trying to write "<<i<<" + "<<j<<endl;
		VectorQueue[i].push(i+j);
	}    
  }
  
  for (int i = 0; i<VECNUM; i++)
  {
	for (int j = 0; j<QUEUENUM; j++)
	{
		cout<<VectorQueue[i].front()<<" ";
		VectorQueue[i].pop();
	}
	cout<<endl;
  }
  return 0;
}
I think you misunderstood the purpose of reserve(). :/

reserve()'s purpose is to reserve memory for the vector to expand, but the vector itself does not expand. You'll want vector::resize(newsize) for that. :)

Happy coding!

-Albatross
You were right, that fixed it !

Thanks
Topic archived. No new replies allowed.