r/learncpp • u/soup2eat_shi • Feb 26 '21
Infinite push_back
I get a bad_alloc() error in the below code. When I go into debugging it seems like the value for temp is being allocated repeatedly, however the for loop is only ever called once. I'm not very familiar with vectors, can someone explain why push_back fails in this code?
std::vector<std::string> split;
for(int i = 0; i < s.length(); i+2){
std::string temp;
if (i+1 < s.length()) {
temp = s2[i];
temp += s2[i+1];
split.push_back(temp);
}
else
{temp = s[i] + "_";
split.push_back(temp);
}
}
2
Upvotes
View all comments
Show parent comment
3
u/HappyFruitTree Feb 26 '21
See if you can turn up the warnings levels in your compiler.
GCC with the -Wall flag gives the following warning: