-2

This is how I declare my vector :

  std::vector <Link *> _components;

Link is this structure :

  struct Link
  {
    size_t targetPin;
    nts::IComponent *component;
  };

First to initialise it I do

  this->_components.reserve(2);

Then, when this instruction happen, it segfault

  this->_components[0]->component = this;

Got an idea ?

Dimitri Danilov
  • 1,338
  • 1
  • 17
  • 45

1 Answers1

2

Reserve expands the capacity of the vector, it does not actually increase the size, and accessing a member beyond size is illegal.

this->_components.resize(2);

is what you appear to need.

If your vector has a fixed size of 2, you may want to consider using std::array instead.

--- EDIT ---

Your second problem is that you haven't assigned a Link* to any of the members. They're null pointers.

You may want std::vector<Link> instead of std::vector<Link*> unless you have Link objects to populate the vector with pointers to.

kfsone
  • 23,617
  • 2
  • 42
  • 74