Mokus
Mokus

Reputation: 10400

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

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?

Upvotes: 5

Views: 2211

Answers (5)

user1610015
user1610015

Reputation: 6678

One difference to watch out for between std::vector and raw arrays is that std::vector will call copy constructors and copy assignment operators for the elements when making copies, whereas with raw arrays you have to do so yourself (e.g. a function like "memcpy" won't call copy constructors/assignment operators).

That's ok if the array's elements are of primitive types or POD types (a POD type, Plain Old Data, is basically a class or struct with only public data members and no base classes).

That difference is exactly why you got better performance with raw arrays: there is less copying going on. C++11 minimizes this problem with rvalue references and move constructors/assignment operators, but it still isn't likely to beat manually-handled memory.

But handling the memory directly everywhere in the code is a bad idea anyway: it can lead to memory leaks, non-exception-safe code, and makes the code overly long and complicated. You can create your own array class that is designed specifically for POD types, that way you'll get both the convenience and safety of an array class like std::vector AND the efficiency of raw arrays.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206566

If you already know the size of the array at compile time use an array, in fact std::array is a better choice but if you need an dynamic array you are better off using std::vector.

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

An array will most likely provide you with better performance because there is no additional indirection needed as in case of an std::vector.

However, note that the advantage in performance in most cases will be not significant in most cases unless your profiler points to that as an problem.

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

You should definitely not be using c-style memory alloocation and management functions in C++. The disadvantages are:

  • With C++, You should'nt be using dynamic allocations at all unless they cannot be avoided. You should rather use smart pointers.This is the very purpose C++ provides them.
  • Also, it is much easier to go wrong with manual memory management. Since you are using C++ future maintainers of your code might just assume you have used new and call delete on a malloced pointer thus causing UB.
  • Another downside of using c-style functions is that unlike new they do not call object constructors, which leaves your objects uninitialized.

Upvotes: 1

Juraj Blaho
Juraj Blaho

Reputation: 13471

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.

Upvotes: 2

Karthik T
Karthik T

Reputation: 31952

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..

Upvotes: 1

SomeWittyUsername
SomeWittyUsername

Reputation: 18368

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).

Upvotes: 2

Related Questions