Std :: vector <double> of destruction throws a cigabra

I have std::vector<double>In which GDB shows this containing these values:

Wph <5 items>            vector<double>
    [0] 10.750281685547618      double
    [1] 0.0053087812248281997   double
    [2] 4.2807534148705719e-08  double
    [3] 5.7427427663508097e-07  double
    [4] 0                       double

With automatic destruction, when a function exits, it returns SIGABRT.

0   raise   raise.c 64  0x7fffeec5ad05  
1   abort   abort.c 92  0x7fffeec5eab6  
2   __libc_message  libc_fatal.c    189 0x7fffeec93d7b  
3   malloc_printerr malloc.c    6283    0x7fffeec9fa8f  
4   _int_free   malloc.c    4795    0x7fffeec9fa8f  
5   __libc_free malloc.c    3738    0x7fffeeca38e3  
6   __gnu_cxx::new_allocator<double>::deallocate    new_allocator.h 95  0x457828    
7   std::_Vector_base<double, std::allocator<double> >::_M_deallocate   stl_vector.h    146 0x45567e    
8   std::_Vector_base<double, std::allocator<double> >::~_Vector_base   stl_vector.h    132 0x4542b3    
9   std::vector<double, std::allocator<double> >::~vector   stl_vector.h    314 0x453a96

What's happening?

  int data = 0;
  vector<double> Wph;
  Wph.resize(mydata.data.size());

  for (size_t t = 0; t < mydata.t.size(); t++)
  {
    double t0 = (PI / 180.0) * mydata.t[i];

    for (size_t p = 0; p < mydata.p.size(); p++)
    {

      double _Wph = 5; //arbitrary math
      Wph[data] = _Wph;

      data++;
    }
  }

struct mydata
{
  vector<double> t, p;
  vector<point> data;
};
+5
source share
3 answers

Please make sure mydata.data.size() == mydata.t.size() * mydata.p.size().

You have assigned values mydata.t.size() * mydata.p.size()to a vector with elements mydata.data.size(). This is an array-related entry.

Maybe you should try it vector::push_back(). I.e

vector<double> Wph;

for (size_t t = 0; t < mydata.t.size(); t++)
{
  double t0 = (PI / 180.0) * mydata.t[i];

  for (size_t p = 0; p < mydata.p.size(); p++)
  {
    double _Wph = 5; //arbitrary math
    Wph.push_back(_Wph);
  }
}
+3
source

data = 5, p = 2, t = 3

  for (size_t t = 0; t < mydata.t.size(); t++)
  {
    double t0 = (PI / 180.0) * mydata.t[i];

    for (size_t p = 0; p < mydata.p.size(); p++)
    {

      double _Wph = 5; //arbitrary math
      Wph[data] = _Wph;

      data++;
    }
  }

, . 6 ... t == 2 p == 1, Wph[5] = _Wph Wph, == 5 4.

0

You make Wph too small, I suspect. We do not know what it is mydata.data.size(), but I assume that it is too small.

From the code, it looks like the right size

  Wph.resize(mydata.p.size() * mydata.t.size());

However, in C ++ we are trying to write security code, so making such errors is more difficult.

  vector<double> Wph;

  for (size_t t = 0; t < mydata.t.size(); t++)
  {
    double t0 = (PI / 180.0) * mydata.t[t];

    for (size_t p = 0; p < mydata.p.size(); p++)
    {
      double _Wph = 5; //arbitrary math
      Wph.push_back(_Wph);
    }
  }

If you must pre-select the size, then at least change

      Wph[data] = _Wph;

to

      Wph.at(data) = _Wph;

at () works just like [], except that at () checks that you haven't passed the end of the array and throw an exception.

0
source

All Articles