C++ - Vector iterator + offset out of range - c++

I have looked at 10 or so answers about the same message but have yet to find my problem.
in level.h in the Level class:
std::vector<Bomb> m_bombs;
in level.cpp:
adding a bomb:
Bomb bomb;
bomb.owner = player;
bomb.power = power;
bomb.x = pos.x;
bomb.y = pos.y;
bomb.timer = 3.0f;
m_bombs.push_back(bomb);
checking bombs (every frame):
void Level::updateBombs(float dt)
{
for(int i = 0; i < m_bombs.size(); i++)
{
m_bombs[i].timer -= dt;
if(m_bombs[i].timer <= 0)
m_bombs.erase(m_bombs.begin() + i);
}
}
I am 100% sure the program crashes on the line where the erase function is called. I am able to output information from the vector which makes this way more confusing. The bombs are correctly in the vector. If I do m_bombs.remove_last() it removes the last element in the vector. Seem to just have problems with erase and begin().

You could fix your code by using erase-remove-idiom.
m_bombs.erase(std::remove_if(m_bombs.begin(), m_bombs.end(),
[](const Bomb& b){ return b.timer < 0.0f;}),
m_bombs.end());
Better split updateBombs function into two function:
updateBombs() does bomb timer update only
removeBombs() removes bombs which expired

Perhaps you can try erasing the vector backwards, e.g. if bomb index 3 and 5 to be erased, you remove the one with index 5 and then 3, because if you erase sequentially from 0 to N-1, by the time you erase index 3, then the index 5 will become index 4, which this can lead to out of bound when reaching the final step of the loop.
try this:
int pivot = 0
void Level::updateBombs(float dt)
{
for(int i = 0; i < m_bombs.size(); i++)
{
m_bombs[i].timer -= dt;
if(m_bombs[i].timer <= 0)
{
m_bombs.erase(m_bombs.begin() + i - pivot);
pivot++;
}
}
}

I had a similar problem when clearing or creating vectors.
The problem occurred because 2 different versions of dlls where being used at runtime. The clearing and creating took place when calling Open CV functions which seem to use different runtime libraries than my visual studio.
Changing visual studio finally fixed the problem (I tried a lot of old OpenCV builds before changing visual studio but to no avail). VS 2008 and 2013 didn't work, VS2010 did.

I would replace m_bombs.size() with an int variable of your own that you decrement after removing a bomb. I've personally had problems with .size(). It's supposed to return the number of elements in the vector but I've had an empty vector and .size() returned numbers greater than zero. Maybe trying this would help narrow down your problem.

Related

C++ time limit exceeded when it doesn't even execute the function

While I was solving a problem in LeetCode, I found something very strange.
I have this line which I assume gives me a time limit exceeded error:
s.erase(i-k, k);
when I comment(//) this line, it doesn't show me time exceed error, but the strange part was, it has never executed even when i didn't comment it.
below is the entire code.
and Here is the problem link.
class Solution {
public:
string removeDuplicates(string s, int k) {
char prev = s[0];
int cnt = 1;
cnt = 1;
for(int i = 1; i < s.size() + 1; i++){
if(s[i] == prev){
cnt++;
} else {
if(cnt == k){
// when input is "abcd" it never comes to this scope
// which is impossible to run erase function.
s.erase(i-k, k);
i = 0;
}
if(i >= s.size()) break;
cnt = 1;
prev = s[i];
}
}
return s;
}
};
When Input is "abcd", it never even go to the if scope where 'erase' function is in.
Although 'erase' function never run, it still affect on the time complexity, and I can't get the reason.
Does anyone can explain this? or is this just problem of LeetCode?
Many online contest servers report Time Exceeding when program encounters critical error (coding bug) and/or crashes.
For example error of reading out of bounds of array. Or dereferencing bad (junk) pointers.
Why Time Exceeded. Because with critical error program can hang up and/or crash. Meaning it also doesn't deliver result in time.
So I think you have to debug your program to find all coding errors, not spending your time optimizing algorithm.
Regarding this line s.erase(i-k, k); - it may crash/hang-up when i < k, then you have negative value, which is not allowed by .erase() method. When you get for example i - k equal to -1 then size_t type (type of first argument of erase) will overflow (wrap around) to value 18446744073709551615 which is defnitely out of bounds, and out of memory border, hence your program may crash and/or hang. Also erase crashes when there is too many chars deleted, i.e. for erase s.erase(a, b) you have to watch that a + b <= s.size(), it is not controlled by erase function.
See documentation of erase method, and don't put negative values as arguments to this method. Check that your algorithm never has negative value i.e. never i < k when calling s.erase(i-k, k);, also never i-k + k > s.size(). To make sure there is no program crash you may do following:
int start = std::min(std::max(0, i-k), int(s.size()));
int num = std::min(k, std::max(0, int(s.size()) - start));
s.erase(start, num);

Removing first three elements of 2d array C++

So here's my problem.. I have a 2d array of 2 char strings.
9D 5C 6S 9D KS 4S 9D
9S
If 3 found I need to delete the first 3 based on the first char.
card
My problem is I segfault almost anything i do...
pool is the 2d vector
selection = "9S";
while(col != GameBoard::pool.size() ){
while(GameBoard::pool[col][0].at(0) == selection.at(0) || cardsRem!=0){
if(GameBoard::pool[col].size() == 1){
GameBoard::pool.erase(GameBoard::pool.begin() + col);
cardsRem--;
}
else{
GameBoard::pool[col].pop_back();
cardsRem--;
}
}
if(GameBoard::pool[col][0].at(0) != selection.at(0)){
col++;
}
}
I've tried a series of for loops etc, and no luck! Any thoughts would save my sanity!
So I've tried to pull out a code segment to replicate it. But I can't...
If I run my whole program in a loop it will eventually throw a segfault. If I run that exact code in the same circumstance it doesn't... I'm trying to figure out what I'm missing. I'll get back in if I figure out exactly where my issue is..
So in the end the issue is not my code itself, i've got memory leaks or something somewhere that are adding up to eventually crash my program... That tends to be in the same method each time I guess.
The safer and most efficient way to erase some elements from a container is to apply the erase-remove idiom.
For instance, your snippet can be rewritten as the following (which is testable here):
using card_t = std::string;
std::vector<std::vector<card_t>> decks = {
{"9D", "5C", "6S", "9D", "KS", "4S", "9D"},
{"9S"}
};
card_t selection{"9S"};
// Predicate specifing which cards should be removed
auto has_same_rank = [rank = selection.at(0)] (card_t const& card) {
return card.at(0) == rank;
};
auto & deck = decks.at(0);
// 'std::remove_if' removes all the elements satisfying the predicate from the range
// by moving the elements that are not to be removed at the beginning of the range
// and returns a past-the-end iterator for the new end of the range.
// 'std::vector::erase' removes from the vector the elements from the iterator
// returned by 'std::remove_if' up to the end iterator. Note that it invalidates
// iterators and references at or after the point of the erase, including the
// end() iterator (it's the most common cause of errors in code like OP's).
deck.erase(std::remove_if(deck.begin(), deck.end(), has_same_rank),
deck.end());
So for anyone else in the future who comes across this...
The problem is I was deleting an element in the array in a loop, with the conditional stop was it's size. The size is set before hand, and while it was accounted for in the code it still left open the possibility for while(array.size() ) which would be locked in at 8 in the loop be treated as 6 in the code.
The solution was to save the location in the vector to delete and then delete them outside of the loop. I imagine there is a better, more technical answer to this, but it works as intended now!
for (double col = 0; col < size; ++col)
{
if(GameBoard::pool[col][0].at(0) == selection.at(0)){
while(GameBoard::pool[col][0].at(0) == selection.at(0) && cardsRem !=0){
if( GameBoard::pool[col].size() > 1 ){
GameBoard::pool[col].pop_back();
cardsRem--;
}
if(GameBoard::pool[col].size() <2){
toDel.insert ( toDel.begin() , col );
//GameBoard::pool.erase(GameBoard::pool.begin() + col);
cardsRem--;
size--;
}
}
}
}
for(int i = 0; i< toDel.size(); i++){
GameBoard::pool.erase(GameBoard::pool.begin() + toDel[i]);
}

Program receives `SIGTRAP` when deallocating vector

I have this code which is supposed to look for elements present in all vectors in a std::vector<std::vector<int>>:
for(int x = 0;x < list.size();x++){
for(int y = 0;y < list[x].size();y++){
std::vector<std::vector<int>::iterator> ptr;
for(int z = 1;z < list.size();z++){
std::vector<int>::iterator iter = std::find(list[z].begin(),
list[z].end(), list[x][y]);
if(iter != list[z].end()){
ptr.push_back(iter);
}else
goto fail;
}
list[0].erase(list[0].begin() + y);
for(int z = 1;z <= ptr.size();z++){
list[z].erase(ptr[z - 1]);
}
fail: continue;
}
}
The program always produces wrong output and randomly crashes. Debugging shows that it receives SIGTRAP when deconstructing list in ntdll. This happens randomly, but the program never works correctly. I don't have any breakpoints in the code.
These are the errors I found in the code some time after asking this question:
The code for determining which iterator is from which array was designed only for list[0], instead of being designed for list[x], causing erase() being called with iterators belonging to another vector, causing memory corruption.
x was replaced with 0 in some places (forgot to change it).
list was being corrupted by other code in the program (fixed now).
These are the fixes:
Used an iterator to std::vector<std::vector<int>::iterator> and if(z==x)continue;else{list[z].erase(*iter); iter++;} to make sure iter always points to the correct element.
Replaced 0 with x.
Fixed problems in other places in the program.
The program works correctly now, thanks for trying to help me.

adding dynamic arrays and removing trailing 0's

I am trying to implement this function to add two dynamic a
rrays, however when I implement this into my main it completely crashes, I have no idea why...
I cannot understand why the program shuts down except the exit code on scite says exit code 255. But that is not helpful. Any idea what the problem may be?
For one:
for (int k=0; k<=max; k++)
This goes out of range. Instead allocate memory for [max+1] elements, since there shall be max+1 terms in the polynomial.
sum = new int[ max + 1 ];
Also, the j loop should start from max.
for (j=max; j>0 && sum[j]==0; --j);
You have a typo on this line:
for (j=max-1; j>0 && sum[j]==0; --j);
^here
The next statement int *tmp=sum; does not get executed.
Also the for loop should probably be
for (j=max-1; j>=0 && sum[j]==0; --j)
^ //don't forget the last member
A couple of nice things about C++ is all the standard containers (like std::vector) and standard algorithms available. For example you could use vectors and backwards iterators and std::find_if_not to find the last non-zero value.
Like
// Create a vector of a specific size, and initialize it
std::vector<int> sum(std::max(a->degree, b->degree), 0);
// Fill it up...
// Find the last non-zero value
auto last_non_zero = std::find_if_not(sum.rbegin(), sum.rend(),
[](const int& value){ return value == 0; });
if (last_non_zero == sum.rbegin())
{
// No zeroes found
}
else if (last_non_zero == sum.rend())
{
// All of it was zero
sum.clear();
}
else
{
std::vector<int> temp(last_non_zero, sum.rend())
std::reverse(temp); // Because the `temp` vector is reversed
sum = temp;
}
After this the vector sum should have been stripped of trailing zeroes.

Vector push_back in while and for loops returns SIGABRT signal (signal 6) (C++)

I'm making a C++ game which requires me to initialize 36 numbers into a vector. You can't initialize a vector with an initializer list, so I've created a while loop to initialize it faster. I want to make it push back 4 of each number from 2 to 10, so I'm using an int named fourth to check if the number of the loop is a multiple of 4. If it is, it changes the number pushed back to the next number up. When I run it, though, I get SIGABRT. It must be a problem with fourth, though, because when I took it out, it didn't give the signal.
Here's the program:
for (int i; i < 36;) {
int fourth = 0;
fourth++;
fourth%=4;
vec.push_back(i);
if (fourth == 0) {
i++;
}
}
Please help!
You do not initialize i. Use for (int i = 0; i<36;). Also, a new variable forth is allocated on each iteration of the loop body. Thus the test fourth==0 will always yield false.
I want to make it push back 4 of each number from 2 to 10
I would use the most straight forward approach:
for (int value = 2; value <= 10; ++value)
{
for (int count = 0; count < 4; ++count)
{
vec.push_back(value);
}
}
The only optimization I would do is making sure that the capacity of the vector is sufficient before entering the loop. I would leave other optimizations to the compiler. My guess is, what you gain by omitting the inner loop, you lose by frequent modulo division.
You did not initialize i, and you are resetting fourth in every iteration. Also, with your for loop condition, I do not think it will do what you want.
I think this should work:
int fourth = 0;
for (int i = 2; i<=10;) {
fourth++;
fourth%=4;
vec.push_back(i);
if (fourth==0) {
i++;
}
}
I've been able to create a static array declaration and pass that array into the vector at initialization without issue. Pretty clean too:
const int initialValues[36] = {0,1,2...,35};
std::vector foo(initialValues);
Works with constants, but haven't tried it with non const arrays.