Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between vector of pointer and vector of values

I heard pointers are not recommended in C++ but I don’t understand why.

My problem is I want to create a vector of class objects to manage my classes.

vector<MyClass> vectorOfClass;

Naturally, for better performance, I should go with a vector of a pointer of class objects?

vector<MyClass *> vectorOfClass;

Or maybe is it possible to create a vector of reference of class objects?

vector<MyClass &> vectorOfClass;

So my questions are :

  • What is the difference between these ways?
  • What is the most optimized to create a vector of class objects?
like image 537
Guillaume Avatar asked Jan 31 '26 03:01

Guillaume


1 Answers

Or maybe is it possible to create a vector of reference of class objects?

No. Reference are not objects in C++. So you can't create arrays of reference or pointer to references. You can however use a std::reference_wrapper, which wraps reference in an object.

What is the most optimized to create a vector of class objects?

Depends always on the situation. Mesure, profile and make your decision according to your data.

What is the difference between these ways?

They are stored in different ways.

A vector of values look like this in memory:

+----------------------+
| std::vector<MyClass> |----
+----------------------+   |
                           |
   -------------------------
   |
   v
+-------------+-------------+-------------+-------------+
|   MyClass   |   MyClass   |   MyClass   |   MyClass   |
+-------------+-------------+-------------+-------------+

Whereas a vector of pointer look like this:

+-----------------------+
| std::vector<MyClass*> |---
+-----------------------+  |
                           |
          ------------------
          |
          v
       +-------+-------+-------+-------+
       |  ptr  |  ptr  |  ptr  |  ptr  |
       +-------+-------+-------+-------+
           |       |      |          |
           v       |      v          |
+-------------+    | +-------------+ |
|   MyClass   |    | |   MyClass   | |
+-------------+    | +-------------+ |
                   v                 v
        +-------------+         +-------------+
        |   MyClass   |         |   MyClass   |
        +-------------+         +-------------+

Both have advantages and disadvantages.

For the value:

  • Pro: Contiguous in memory. There is no pointer chasing and usually really fast to iterate.
  • Pro: Automatic memory managemen. Vector will manage the memory of every values it allocates.
  • Con: Reference invalidation. Resizing the vector will invalidate every reference to the objects inside it.
  • Con: In slower to resize with non-trivial objects. Resizing involve moving objects around. That may be slower with large or non-trivial objects.

For pointer:

  • Pro: No reference invalidation. The address of the object are managed by you.
  • Pro: Faster reallocation, since it will be the cost of copying pointers around instead of moving objects around.
  • Con: Slow iteration. For each element in the vector, the CPU will have to ask the memory for it and cannot use its cache efficiently.
  • Con: You must use std::unique_ptr to own the memory, and most likely allocate every objects distinctly. Allocating a large amount of distinct objects is slow.

The default choice should be std::vector<MyClass>. This is by far the simplest and work for most cases. Usually when I need references to those objects, I tend to use index in the vector which are stable as long as there are no element removed in the middle.

like image 99
Guillaume Racicot Avatar answered Feb 01 '26 18:02

Guillaume Racicot