Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any disadvantages if I'm using Array instead of Vector?

Tags:

c++

c

memory

I wrote a MPC controller in C++, which included a Matrix class, where I stored the data in an array and I used C memory functions(memcpy, memset etc). Today I replaced the array with c++ vector and I used copy to move the memory etc... I faced one problem, by replacing the array with vector the calculation time of the control signal almost doubled by using vector.

Is there any disadvantages if I'm using alloc,memcpy,memset, free ins c++ code? If there is any what are those?

like image 926
OHLÁLÁ Avatar asked Nov 26 '12 08:11

OHLÁLÁ


People also ask

What is advantage of using vectors instead of arrays?

Reserve space can be given for vector, whereas for arrays you cannot give reserved space. A vector is a class whereas an array is a datatype. Vectors can store any type of objects, whereas an array can store only homogeneous values.

Is array more efficient than vector?

A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.

Are arrays more flexible than vectors?

A: Almost always [use a vector instead of an array]. Vectors are efficient and flexible. They do require a little more memory than arrays, but this tradeoff is almost always worth the benefits. That's an over-simplification.


3 Answers

Vector adds value in 2 aspects:

  1. Provides additional functionality which is not present in C array, such as resizing, checking for current size etc. You may find better alternatives from its interface to your custom solution.
  2. Catches some bugs, such as addressing an address beyond the scope of the vector.

If you are satisfied with your proprietary data management and confident in quality of your code, you don't need vector. Note, though, this might pose some software issues, such as decreased maintainability of C array compared to vector (e.g., future code may access an out of bounds value, even if your current code doesn't).

Edit: See @Als answer for a possible alternative in your case (std::array).

like image 97
SomeWittyUsername Avatar answered Nov 30 '22 02:11

SomeWittyUsername


Using manual memory management means you have more work to do. Therefore you have more opportunity to make errors.

Manual memory management is almost always wrong in C++. If you really need a naked array, you should at least use a wrapper like boost::scoped_array or boost::shared_array. Also std::unique_ptr can be used to manage arrays.

like image 28
Juraj Blaho Avatar answered Nov 30 '22 01:11

Juraj Blaho


Vector has 1 major(relatively speaking) problem if you are inserting or deleting many objects, besides the case of inserting in begining/middle of the vector which is irrelevant here (array has same issue).

If you have large number of inserts to be done, you would typically add a call to reserve before the inserts. Call it as vector.reserve(<best guess number of objects to be inserted>)

This is because, as you would know, vector allocates memory on an as needed basis. If you are inserting a large number of items at a time, that might involve a lot of reallocations/copies which might consume a lot of time.

There are other solutions to this problem. One is to use std::deque which is a little more efficient in such cases as it does not copy over the entire vector every time. Or to use std::array which is a vector style wrapper around a C array.

Edit: The title has changed, but this information might still be useful..

like image 33
Karthik T Avatar answered Nov 30 '22 03:11

Karthik T