new double[x] vs vector<double> (x) where integer x is user specified
Suppose in a code I want the user to specify an integer(say x) and then allocate those many开发者_如何转开发 consecutive cells in computer memory on which I do some kind of processing.
My question is is it "better" to allocate this memory as new double[x]
or vector<double>(x)
.
I know that both methods seem to work fine ( i.e. compiles and runs at least in this teeny little code)
int x; int* p;
cout<<"Enter integer x:";cin>>x;
vector<double> v(x);
p=new double[x]
If you think the answer is using new
then is it possible to somehow magically convert
the memory allocated via new
into a vector? Because vectors would give me more power in manipulating the data.
It's better to use vector
whenever you can because of the many advantages it provides (automatic memory management, bounds checking, etc).
Also, to have the vector reserve a number of elements for you to use (i.e. allocate that many all at once in memory instead of eventually resizing up to it as you add elements), you can pass that number to the vector's constructor:
int x;
cout<<"Enter integer x:";cin>>x;
vector<double> v(x);
Or if you've already created a vector, use vector::resize
:
// created v somewhere up here and we want to reuse it
int x;
cout << "Enter integer x: "; cin >> x;
v.resize(x);
And no, there's no way to tell a vector to use memory you've already allocated unless you use a custom allocator [R. Martinho Fernandes, comments].
As the others have mentioned, it's normally always better to use a vector. However, to give a counterpoint, here are some possible situations when an array might be better:
- Memory is extremely limited (for example, in embedded programming with simple microcontrollers). Vector generally reserves a larger array than is required to store its elements, and also has some additional memory overhead to keep track of things like number of items in the list.
- You need to interoperate with C code that has no understanding of the vector class. In that case, you will need to copy your data from the vector to an array, pass the array to the C code, and then deallocate the array once the C code is done with it.
- You know the exact number of elements in the list, and that number will never change. Even in this case, it's probably better to use a vector.
If you must use an array, it's best to allocate it on the stack if possible. For example...
char my_array[ 100 ]; // allocated on stack - better
char* my_array_2 = new char[ 100 ]; // allocated on heap - worse
The first example is better because the memory will automatically be reclaimed once your code leaves the block that the array is defined in. However, you can only allocate on the stack if you know in advance the size of the array. If the size may change at runtime, you must allocate on the heap, unfortunately.
int x = 20 + some_num;
char my_array[ x ]; // error - won't compile
You should always prefer vector over new, because it absolves you of worrying about memory management. Vector uses new under the covers but hides the details from you.
There's no way to make vector use a buffer that you've already allocated with new, and there's really no reason to try.
精彩评论