0

Anybody know why i cant assign values to each num_episodio vector in the current struct i am in?

#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct serie{
vector<int> num_episodio;

};

int main(){
  //create a vector of structs

vector<serie> serie_individual;


serie_individual.push_back(serie());

serie_individual[0].num_episodio[0]=1;

cout<<serie_individual[0].num_episodio[0];

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Please indent your code properly and [don't use `using namespace std;`](https://stackoverflow.com/q/1452721/430766). – bitmask Jun 06 '20 at 18:49
  • The easy way to debug segfaults is to following this [instruction](https://www.cee.studio/segfault.html) – stensal Jun 06 '20 at 21:11

3 Answers3

4

serie_individual[0].num_episodio is an empty vector. serie_individual[0].num_episodio[0] exhibits undefined behavior by way of accessing an index out of bounds.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • ¿What would be the correct way of populating this vector? – Samuel .R.F Jun 06 '20 at 18:39
  • 3
    We leave it as an exercise for the reader. You managed to populate `serie_individual`, so you should know enough to answer your question. – Igor Tandetnik Jun 06 '20 at 18:42
  • @Samuel.R.F The thing that you did to make `serie_individual[0]` valid might be useful if you want to make `serie_individual[0].num_episodio[0]` valid. – bitmask Jun 06 '20 at 18:48
1

see that just because you are pushing an element in the serie_individual doesnt mean the vector num_episodio in that element is filled with data... then when you do:

serie_individual[0].num_episodio[0]

you are trying to get the 1st element of an empty vector...

that is the cause of the error in your code...

one option to fix that is to define a constructor in the serie and insert in its vector like:

struct serie
{
    serie(int i)
    {
        num_episodio.push_back(i);
    }
    std::vector<int> num_episodio;
};

and you have to adapt your code like:

int main()
{
    struct serie
    {
        serie(int i)
        {
            num_episodio.push_back(i);
        }
        std::vector<int> num_episodio;
    };
    std::vector<serie> serie_individual;

    serie_individual.push_back(serie(2));
    std::cout << "serie before: " << serie_individual[0].num_episodio[0] << std::endl;
    serie_individual[0].num_episodio[0]=10;
    std::cout << "serie after: " << serie_individual[0].num_episodio[0];
    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

It's actually a dereference of null pointer error at the following line, where num_episodio is NULL.

serie_individual[0].num_episodio[0]=1;  

serie_individual[0].num_episodio[0]=1 is equivalent to *(serie_individual[0].num_episodio)=1;

The following is the memory error that you can reproduce/debug with this link

  Memory access error: dereferencing a null pointer; abort execution.
  # Writing 4 bytes to address 0x0.
  #
  # Stack trace (most recent call first) of the write.
  # [0]  file:/a.cc::19, 1
  # [1]  [libc-start-main]
stensal
  • 401
  • 4
  • 8