I'm currently programming something needing vectors. I have two nested loops;
for (size_t i = 0; i < m_neurons.size(); i++) {
for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
}
}
This section is the one we are interested in:
d.out ("i == " + std::to_string (i));
d.out ("m_neurons.size() == " + std::to_string (m_neurons.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
I print out some sizes and then, I want C++ to loop through the vector; easy enough, right?
Except this happens:
i == 0
m_neurons.size() == 1
m_neurons[i].m_weights.size() == 4
m_neurons[i].m_weights.size() == 4
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (Speicherabzug geschrieben)
[maximilian@new-host library]$
1) Somehow the vector gets emptied
2) Somehow, the collection of the data is treated like I used .at (int index)
I have:
- tried alternating between
vector.at()
andvector[]
- deleted the executable and recompiled (multiple times)
- rebooted my pc [dunno, might help]
- investigated with GDB (the vector changed value from one step to the other)
- Used my mediocre googling skills to search something similar.
- Played a bit with the content of those vectors
I know that:
- The vector is NOT changed in between
- It is exactly THIS line of code that bugs out [because I have a printing s tatement exactly after]
- I recall it working a bit earlier in the day
Now my Questions are:
- Is this a known issue/feature?
- What caused it?
- How can I fix it?
EDIT:
I made some code that boils down the problem. Seems to work fine. The thing is: NOTHING at all changes between my code and the one there. What could be the thing that triggers the bug? As said: in dbg, the size of the vector changes from one step to the other
#include <iostream>
#include <vector>
#include "debugMachine.h"
class bar {
public:
bar (std::vector<double> i) {
foobarmiz = i;
}
std::vector<double> foobarmiz;
};
class foo {
public:
std::vector<bar> foobar;
void test (std::vector<double>& expected) {
d.out ("HI");
for (size_t i = 0; i < foobar.size(); i++) {
d.out ("HI2");
d.out ("i == " + std::to_string (i));
d.out ("m_neurons.size() == " + std::to_string (foobar.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (foobar.at(i).foobarmiz.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (foobar.at(i).foobarmiz.size()));
for (size_t j = 0; j < foobar[0].foobarmiz.size(); j++) {
d.info ("Gonna change weights");
foobar.at(i).foobarmiz[j] +=1;
}
d.out ("out of backward");
}
}
} f;
int main () {
d.write = true;
std::vector<double> test {1.0f, 2.0f, 3.0f, 5.34f};
f.foobar.push_back (bar (test));
f.foobar.push_back (bar (test));
f.foobar.push_back (bar (test));
f.test (test);
}
Here is the entirity of the function that bugs out:
void CLayer::m_backward (std::vector<double>& expected) {
d.out ("HI");
for (size_t i = 0; i < m_neurons.size(); i++) {
d.out ("HI2");
d.out ("i == " + std::to_string (i));
d.out ("m_neurons.size() == " + std::to_string (m_neurons.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
d.info ("Gonna change weights");
d.info ("m_weights before : " + std::to_string(m_neurons.at(i).m_weights.at(j)));
m_neurons.at(i).m_weights[j] -= (0.003f * -(m_expected.at(i) - m_output.at(i)) * m_output.at(i) * (1 - m_output.at(i)) * m_input.at(j));
d.info ("m_weights after : " + std::to_string(m_neurons[i].m_weights[j]));
}
d.out ("out of backward");
}
}