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?
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.
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.
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.
Vector
adds value in 2 aspects:
C
array, such as resizing, checking for current size etc. You may find better alternatives from its interface to your custom solution.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
).
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.
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With