how to allocate vector if the size is a variable


The size of this vector is large, read by configfile.

int j;
std::vector<double> *p = new std::vector<double> [j];

gcc pass, but run in segment fault.
how?
Last edited on
You are creating a dynamic array of vectors, try something like this:
1
2
int j = /*Some Value*/ ;
std::vector<double> p( j );


http://www.cplusplus.com/reference/stl/vector/vector.html
Last edited on
thanks, blush.
Of course, the value used to initialize the vector must have an actual value. Using an uninitialized integer to construct the vector isn't a good idea. The OP might think this obvious but other beginners might someday find this thread and read it. I agree with Bazzy's point that operator new was not needed here at all. I'd just like to see in the example that j has to be filled with a meaningful value first.
Edited my post above
Topic archived. No new replies allowed.