While loop not equaling std::queue size - c++

I have this code:
std::queue<unsigned int> offsets;
// (fill offsets here)
DEBUG(std::to_string(offsets.size())) // print offsets.size() to console
int iterations = 0;
while (!offsets.empty())
{
iterations++;
unsigned int currOffset = offsets.front();
offsets.pop();
if (currOffset == 0)
{
DEBUG("breaking from while loop")
break;
}
// do something with currOffset
}
DEBUG(std::to_string(iterations))
For some reason, iterations never equals offsets.size(). I'm not sure why this is. In my test application, offsets.size() == 28, but iterations == 11. I only break from the while loop once in this application.
Any idea why this is happening? Help is greatly appreciated.

Because the 11th offset is zero and the conditional break triggers before the loop reaches the end of your data structure?
Either that or // do something with currOffset involves popping more things from the queue.

The if breaks the loop if front() == 0, doesn't need be empty.
while (!offsets.empty())
{
iterations++;
unsigned int currOffset = offsets.front();
offsets.pop();
if (currOffset == 0) // *** Here is the problem ***
{
DEBUG("breaking from while loop")
break;
}
// do something with currOffset
}

Related

Calling functions randomly using a vector

#assume everything needed is included
void Robot::moveRobot()
{
//calls a random directon for robot to move in
//if direction returns false (not able to move in that direction),
//call another random direction up to 4 times, excluding the one(s)
//already called. If they all return false, do not move the robot.
//vecDir = {moveForward(), moveBackward(), moveRight(), moveLeft()}
// = {0,1,2,3} initially
vector<int> vecDir{0,1,2,3}; //vetor indicating direction to move
int num = rand() % vecDir.size();
if(num == vecDir[0])
{
//if not able to move forward, try a different random direction
if(Robot::moveForward() == false)
{
vecDir.erase(num);
//here, vector will be vecDir={1,2,3}
}
}
else if(num == vecDir[1])
{
Robot::moveBackward();
}
else if(num == vecDir[2])
{
Robot::moveRight();
}
else //num == vecDir[3]
{
Robot::moveLeft();
}
}
Hi! I'm trying to randomly call these four functions within the moveRobot() function using a vector whose size is changed depending on if a direction cannot be called. I set moveForward() to the first element, moveBackward() to the second element, etc. If any of the moveXXXX() functions are false, I want to delete that element of the array. Example code shown
Example output:
//before doing anything, vecDir = {0,1,2,3}
int num = rand() % vecDir.size(); //assume num = 1, so it calls moveBackward()
//assume moveBackward() is false, so gets rid of that element
vecDir.erase(num); //new vecDir = {0,2,3};
// vecDir(0) would be moveForward(), vecDir(1) is now moveRight(), vecDir(1) is now moveLeft()
How would I continue this process to exhaust all elements and not move a robot? I know a for loop would be involved, but I cannot think of where to use it. I am also not sure if my thinking is correct by using if else for each element. Any help is appreciated, and I apologize if the question is confusing. I can clear it up if there are any misunderstandings.
Just have a vector of function pointers, rather then numbers.
void Robot::moveRobot() {
// vector of pointers to functions to move
std::vector<bool()> moves{
moveForward(), moveBackward(), moveRight(), moveLeft()
};
// we repeat the process until any moves are available.
while (moves.size() > 0) {
// pick a random move
const int num = rand() % moves.size();
// try to move
if (moves[num]() == true) {
// yay, we moved!
break;
}
// we did not move - remove current option and repeat
moves.erase(moves.begin() + num);
}
}

Decrementing an index in multiple threads

In order to process data in an array using multithreading, I'd like to access each element of the array using an index. Each thread decrements the index and uses its current value to process the corresponding data in the array. The index is an atomic integer and decremented until it's -1 (or 0xFF). How can I prevent the value of the index to become less than -1?
data_type myData[MAX_DATA_COUNT];
std::atomic<uint16_t> data_index;
void process_array()
{
uint16_t index = data_index.fetch_sub(1); // problem starts here!
//
if(index != -1)
{
do_something_with(myData[index]); // process data at index
}
else
{
data_index = -1;
}
}
void worker_thread()
{
while(is_running){
wait_for_data();
process_array();
}
}
The problem is that multiple threads can subtract 1 from data_index and make it less than -1. How can I do this?
Use compare_exchange method. This is a standard way for modify variable only after successfull check.
void process_array()
{
uint16_t index = data_index.load();
while((index != -1) && !data_index.compare_exchange_weak(index, index - 1));
if(index != -1)
{
do_something_with(myData[index]); // process data at index
}
}

Finding path in a 2-d maze

Why is this code giving run-time error. I am trying to find if their exist a path inside the maze to reach the food(2). 0 representing obstacle, 1 representing path, and 2 is for the destination.
`{0,1,1,0,0},
{1,0,1,1,1},
{0,0,0,0,0},
{0,1,1,1,1},
{0,0,1,1,0},
{1,0,1,1,2}`
I'm passing the starting point as findpath(a,3,2) where a is the maze and i=3, j=2 as the starting point.
Code on ideone: http://ideone.com/poF9um
Thanks guys, for helping me. I have rectified my mistake.
Here's the ideone link for the updated code: http://ideone.com/poF9um
Thanks.
#include <iostream>
using namespace std;
/*int insideMaze(int x, int y){
if(x>=6 || y >=5 || x<0 || y< 0)
{
return 0;
}
return 1;
}*/
bool findPath(int a[6][5], int n1, int m1){
if(n1>=6 || m1 >=5 || n1<0 || m1<0){
return false;
}
if(a[n1][m1] == 0){
return false;
}
if(a[n1][m1]==2){
return true;
}
//a[n1][m1] = 4;
if(findPath(a,n1-1,m1)==true){
return true;
}
if(findPath(a,n1,m1-1)==true){
return true;
}
if(findPath(a,n1+1,m1)==true){
return true;
}
if(findPath(a,n1,m1+1)==true){
return true;
}
return false;
}
int main() {
// your code goes here
//int a[10][10];
int a[6][5] = {
{0,1,1,0,0},
{1,0,1,1,1},
{0,0,0,0,0},
{0,1,1,1,1},
{0,0,1,1,0},
{1,0,1,1,2}
};
if(findPath(a,3,2)){
cout<<"Path Found";
}
return 0;
}
The problem is caused by stack overflow. Your depth-first search does not mark locations that it visits, so the same location would be visited multiple times.
You start at (3, 2), and try going left.
This takes you to (3, 1).
There is no path left of (3, 1), so you go right.
This takes you back to (3, 2), from where you try to go left.
This takes you to (3, 1).
There is no path left of (3, 1), so you go right...
See the problem? To fix it, add another array of the spots that you have visited, and check it before continuing with the search. This will fix the issue.
I guess that the code causes infinite recursive calls.
You begin with executing findPath(a,3,2).
Since a[3][2]==1, the code will call findPath(a,3,1).
Now, since a[3][1]==1, the code will later call again to findPath(a,3,2) again...
And so on... Until you run out of memory / stack overflow...

Trouble removing elements from C++ vector

I'm trying to remove 'dead' bullets from my vector of bullets. Every frame, I'm calling the Bullet::update() function which looks like this:
void Bullet::update()
{
for(int i = 0; i != mAmmo.size(); i++)
{
if(mAmmo[i].sprite.getPosition().x > 700)
mAmmo[i].mAlive = false;
if(mAmmo[i].mAlive == false)
{
// I get a Debug Assertion Failed at runtime from this piece of code
mAmmo.erase(mAmmo.begin()+i);
}
if(mAmmo[i].mAlive == true)
{
mAmmo[i].sprite.move(mMovement);
}
}
}
Am I doing this completely incorrectly? This is the first time I've really used vectors more than just following through a tutorial. If I need to post any more code, just tell me. I've been working on this for the past few hours, so I'm a wee bit desperate to get this to work.
Thanks in advance!
You're easily walking into undefined behavior as soon as the ith element is the last element in your list. Use iterators, and pay special attention to the return value of erase(), as it automatically advances the iterator for you so your loop doesn't have to.
void Bullet::update()
{
for (auto it = mAmmo.begin(); it != mAmmo.end();)
{
if(it->sprite.getPosition().x > 700)
it->mAlive = false;
if (!it->mAlive)
{
// erase and get next iterator
it = mAmmo.erase(it);
}
else
{ // move and increment
it->sprite.move(mMovement);
++it;
}
}
}

C++ Break out of a vector loop

I have a method in a class LinkRepository, I am checking for duplicate entries in the vector array Datalinks, which is a member of the class. I loop through all the elements in the array to check in the new entry Datalink* datalink already exist in the array. If so then don't add, just exit the loop.
void LinkRepository::SaveLink(Datalink* datalink) {
bool Exist = false;
for(vector<Datalink*>::iterator dl = Datalinks.begin(); dl != Datalinks.end(); ++dl)
{
if((strstr((*dl)->ParentID, datalink->ParentID) != NULL) && (strstr((*dl)->ChildID,datalink->ChildID) != NULL))
{
Exist = true;
dl = Datalinks.end();
}
}
if(!Exist)
{
Datalinks.push_back(datalink);
}
};
My program seems to crash on the next loop of the statement dl = Datalinks.end();
I have no idea why it is crashing?
Replace
dl = Datalinks.end();
With:
break;
To exit the loop
Here is a simple example to illustrate why your solution can't work:
int i = 0;
for (; i != 10; ++i)
{
i = 10;
}
This loop will never end because i will be incremented to 11 before comparison i != 10
It is crashing because first you set the iterator to Datalinks.end() and then, upon leaving this iteration, the for loop itself increments the iterator, making an invalid operation.
for(vector<Datalink*>::iterator dl = Datalinks.begin(); dl != Datalinks.end() && !Exist; ++dl)
{
if((strstr((*dl)->ParentID, datalink->ParentID) != NULL) && (strstr((*dl)->ChildID,datalink->ChildID) != NULL))
{
Exist = true;
}
}
Like everyone had said you are iterating one over. So, it's going into unwanted memory locations resulting in a seg fault eventually. You have to realize that the ++dl is happening at the end of the loop.
Also, using a break statement here is ridiculous. You already have a bool, make use of it.