About the random number in snake games - c++

#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
int map_height=22;
int map_width=22;
string border="ùþ";
int snakeheadX=map_width/2-1;
int snakeheadY=map_height/2;
string head_symbol="¡·";
int snakesize;
bool gameover=false;
enum action{up, down, right1, left1, stop};
action moving=stop;
int foodx;
int foody;
void food();
void draw(){
system("cls");
food();
for(int i=0;i<map_width;i++){ // Game frame
cout<<border;
}
cout<<endl;
for(int i=0;i<map_height;i++){
cout<<border;
for(int j=0;j<map_width-2;j++){
if(i==snakeheadY && j==snakeheadX){
cout<<head_symbol;
}
else if(i==foody && j==foodx){
cout<<"¡¯";
}
else
cout<<" ";
}
cout<<border;
cout<<endl;
}
for(int i=0;i<map_width;i++){
cout<<border;
}
}
void running(int moving){
if(moving==up){ //Up
snakeheadY--;
}
if(moving==down){ //Down
snakeheadY++;
}
if(moving==left1){ //Left
snakeheadX--;
}
if(moving==right1){ //Right
snakeheadX++;
}
if(moving==stop){
snakeheadX=snakeheadX;
snakeheadY=snakeheadY;
}
}
void food(){
int prev_foodx=foodx;
int prev_foody=foody;
int a=rand()%(map_width-3)+1;
int b=rand()%(map_height-1)+1;
if(foodx==snakeheadX && foody==snakeheadY){
if(prev_foodx==a && prev_foody==b){
foodx=rand()%(map_width-3)+1;
foody=rand()%(map_height-1)+1;
}
else{
foodx=a;
foody=b;
}
}
}
void move_logic(int getkeyboarddown){
if(getkeyboarddown=='w'){ //Up
snakeheadY--;
moving=up;
}
if(getkeyboarddown=='s'){ //Down
snakeheadY++;
moving=down;
}
if(getkeyboarddown=='a'){ //Left
snakeheadX--;
moving=left1;
}
if(getkeyboarddown=='d'){ //Right
snakeheadX++;
moving=right1;
}
}
void game_logic(){
if (snakeheadX>=map_width || snakeheadX<=0 || snakeheadY<=0 || snakeheadY>=map_height){
cout<<endl<<"Game over!";
gameover=true;
}
}
int main()
{
srand(time(0));
foodx=rand()%(map_width-3)+1;
foody=rand()%(map_height-1)+1;
while(!gameover){
Sleep(50);
cout<<foodx<<" "<<foody;
running(moving);
draw();
game_logic();
if(kbhit()){
move_logic(getch());
}
}
return 0;
}
The code above is my code of snake game in C++. Although the game is not finished, I found the food may sometimes appear in same location after the snake ate. Therefore, in the function food(), I add a script of code below to prevent this state.
if(foodx==snakeheadX && foody==snakeheadY){
if(prev_foodx==a && prev_foody==b){
foodx=rand()%(map_width-3)+1;
foody=rand()%(map_height-1)+1;
cout<<"ok";
}
else{
foodx=a;
foody=b;
}
But I still found the food sometimes appear in same location after the snake ate that means the code I wrote is useless. How can I solve this problem?

The problem is in your move_logic and running functions. You aren't generating the food in the same position twice in a row by chance, what is actually happening is that when the player changes direction the snake actually moves twice, causing it to jump over a position. If you are passing by the food and change direction to get the food, you will actually jump over the food instead.
E.g. Starting with snakeheadX = 2, snakeheadY = 5 and moving = left1, say the player presses s to change direction. move_logic() increments snakeheadY to 6, then running() increments snakeheadY to 7, and only after that are draw() and food() called, so if the food was in position (2,6), then it was missed, and it looks like the snake went right through it, or like new food was generated in the same position.
To fix this, function move_logic() should not modify snakeheadX or snakeheadY. Only the running() function should modify them.
Aside from that, it looks like it should work, but there's a lot that could be cleaned up. E.g. You should use chained else if statements in functions running() and move_logic(). In function food(), prev_foodx and prev_foody aren't actually doing anything useful, because foodx and foody haven't changed at the time when prev_foodx and prev_foody are checked. food() also generated the random positions every time it is called, rather than only when necessary, and does allow food to be generated twice in the same position, albeit only with a chance of 1/22^4. So the randomness could be better implemented using a loop, like this:
void food() {
if (foodx == snakeheadX && foody == snakeheadY) {
int new_foodx;
int new_foody;
do
{
new_foodx = rand() % (map_width - 3) + 1;
new_foody = rand() % (map_height - 1) + 1;
} while (new_foodx == foodx && new_foody == foody);
foodx = new_foodx;
foody = new_foody;
}
}

I don't see what's causing you're issue, but one change you can make to prevent the code from randomly generating the same x and y coordinates two times in a row is to replace the code
if(prev_foodx==a && prev_foody==b){
foodx=rand()%(map_width-3)+1;
foody=rand()%(map_height-1)+1;
}
with
if(prev_foodx==a && prev_foody==b){
food();
}
The food function would then keep calling itself until it comes up with new x and y coordinates for the food. You can also optimize the function so it is not generating random numbers every time the screen is drawn by moving the if statement above the variables you're declaring like so.
void food(){
if(foodx==snakeheadX && foody==snakeheadY){
int prev_foodx=foodx;
int prev_foody=foody;
int a=rand()%(map_width-3)+1;
int b=rand()%(map_height-1)+1;
if(prev_foodx==a && prev_foody==b){
food();
} else{
foodx=a;
foody=b;
}
}
}
Also the name food does not tell me what is going on at all. Perhaps in the draw function you could say
if(foodx==snakeheadX && foody==snakeheadY){
changeFoodPos();
}`
and then instead of your food function, you have a changeFoodPos function. It's always better to have functions that are for one specific purpose like changing the position of food, than to you have a function that could be doing anything with food. Also, you don't need the prev_foodx and prev_foody variables. You could just do
void changeFoodPos(){
if(foodx==snakeheadX && foody==snakeheadY){
int newX = rand()%(map_width-3)+1;
int newY = rand()%(map_height-1)+1;
if(foodx==newX && foody==newY){
changeFoodPos();
} else{
foodx = newX;
foody = newY;
}
}
}
Anyhow, I know this doesn't directly answer you're question. It looks like David Scarlett was able to figure out what the issue was, but I hope this helps you out as you're learning to code.

Related

My game of Snake crashes when you eat an apple, and the apple spawns inside your body after picking a new position (C++, SDL)

I thought I'd attempt to make Snake since it is a pretty easy game to make. I was having an issue where the apple would spawn inside the snakes body, and so I came up with a way to prevent that from happening:
void getRandomApplePos() {
// variable that tells the while loop whether the moving of the apple was successful
bool success;
// variable that is set to false if the apple is inside the snakes body
bool appleNotInside;
// Tells the collision to stop testing for collision until the apple has successfully moved
bool appleHasMoved;
// sets the variables
success = false;
appleNotInside = false;
appleHasMoved = false;
// while the apple spawns inside the snake, it keeps generating new positions
while (!success) {
// random seed
srand((unsigned int)time(NULL));
// gets a random position
int randomX = rand() % 769;
int randomY = rand() % 673;
// resets the two variables if this while loop as ran again
apple.delta_pos_x = 0;
apple.delta_pos_y = 0;
// checks to see if the apple has spawned in the same exact position
while (apple.delta_pos_x == 0 && apple.delta_pos_y == 0) {
// gets the previous poition of the apple
apple.prevPos_x = apple.x;
apple.prevPos_y = apple.y;
// picks a new apple position
apple.x = round((randomX) / 32) * 32;
apple.y = round((randomY) / 32) * 32;
// gets the new apple position
apple.currentPos_x = apple.x;
apple.currentPos_y = apple.y;
// sets the difference between the positions, if it's 0, then it has spawned in the same exact location
apple.delta_pos_x = (float)(apple.currentPos_x - apple.prevPos_x);
apple.delta_pos_y = (float)(apple.currentPos_y - apple.prevPos_y);
}
// checks to see if the snake length is only one, as to make the list not go out of index
if (snake.bodyLength == 1) {
// if the apple happens to spawn inside the snake with a length of 1, it will add false to the appleInSnake vector, else it adds true
if (apple.x == snakeBody[0][0] && apple.y == snakeBody[0][1]) {
appleNotInside = false;
appleInSnake.push_back(appleNotInside);
}
else {
appleNotInside = true;
appleInSnake.push_back(appleNotInside);
}
}
else {
// if the apple happens to spawn inside the currently compared snakeBodyPosition, it will add false to the appleInSnake vector, else it adds true
for (int i = 0; i < snakeBody.size(); i++) {
if (apple.x == snakeBody[i][0] && apple.y == snakeBody[i][1]){
appleNotInside = false;
appleInSnake.push_back(appleNotInside);
}
else {
appleNotInside = true;
appleInSnake.push_back(appleNotInside);
}
}
}
// if false appears inside the appleInSnake vector at all, it sets success to false and goes through the loop again. Else it breaks out.
if (std::find(appleInSnake.begin(), appleInSnake.end(), false) != appleInSnake.end()) {
success = false;
}
else {
success = true;
}
//clears appleInSnake so that it can take in a new comparision
appleInSnake.clear();
}
// tells the collision to start back up again
appleHasMoved = true;
}
So, whenever the apple does end up spawning inside of the snakes body, it crashes, just outright. I suspect some kind of infinite loop, but I can't put my finger on why this happens.
You are initializing your random number generator within your loop.
Note that the RNG is deterministic. It means that you will end up drawing the same numbers all over again as in the previous loop.
Initialize the RNG once at the start of your program. This way, the numbers drawn may be expected to be different within every loop.
You might wonder, that the crude use of time() should prevent this. A typical implementation of time() will have the granularity of seconds. So you would only expect the return value to change once a second, hence, you get the same initialization over and over again in your loop.

return does not stop function, Recursive function issue? (programming exercise, Dynamic Programming, Levenshtein Back-trace)

the printOptimalAlignment function is misbehaving. goto and return will not exit when the function reaches location (1,1)... where it should end, no crash and it stops at seemingly an arbitrary location of (6,6)... because for some reason it increments at the end of the function even though there is no increment-er for the values int yL, int xL, (but I don't follow why it calls itself if it gets to the end of the function without any "hits" on the if statements.
Full code:
https://repl.it/#fulloutfool/Edit-Distance
void printOptimalAlignment(int** arr, string y, string x,int yL, int xL){
int I_weight=1, D_weight=1, R_weight=1;
bool printinfo_allot = 1,printinfo = 1;
if(printinfo_allot){
cout<<"Location: "<<"("<<xL<<","<<yL<<")"<<"-------------------------------\n";
cout<<"Same check Letters: "<<x[xL-2]<<","
<<y[yL-2]<<"("<<(x[xL-2] == y[yL-2])<<")"<<"\n";
cout<<"LL: "<<"("<<xL-1<<","<<yL<<")"
<<":"<<arr[yL][xL-1]
<<":"<<(arr[yL][xL-1]+I_weight)
<<":"<<(arr[yL][xL])
<<":"<<(((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1)
<<":"<<(yL>=1 && xL>=1)<<"\n";
cout<<"xL state:"<<((&x[xL]))<<":"<<(x[xL-1])<<"\n";
cout<<"yL state:"<<((&y[yL]))<<":"<<(y[yL-1])<<"\n";
string tx = &x[xL];
cout<<x.length()<<","<<(tx.length()+1)<<"\n";
}
string tx = &x[xL]; // slopy hotfix
if(x.length()==(tx.length()+1)){
cout<<"return functionality not working?-=-=-=-=-=-=-=-=\n";
cout<<"-> Prep last, current distance = "<<arr[yL][xL] <<"\n";
return;
//printOptimalAlignment(arr,y,x,yL-1,xL-1);
//cant use this goto... but where does it go?
//goto because_Im_a_terrible_person;
throw "how?... breaking rules... make it stop";
}
if(yL>=1 && xL>=1 && (x[xL-2] == y[yL-2]) == 1){
if(printinfo){
cout<<"-> Same (same char), current distance = "<<arr[yL][xL] <<"\n";
}
printOptimalAlignment(arr,y,x,yL-1,xL-1);
}
if(yL>=1 && xL>=1 && (arr[yL-1][xL-1] == arr[yL][xL])){
if(printinfo){
cout<<"-> Swap (same int), current distance = "<<arr[yL][xL] <<"\n";
if(arr[yL-1][xL-1]==0)cout<<"---this is last---\n";
}
printOptimalAlignment(arr,y,x,yL-1,xL-1);
}
if(yL>0 && xL>0 && (arr[yL-1][xL]+D_weight == arr[yL][xL])){
if(printinfo){
cout<<"-> Delete, current distance = "<<arr[yL][xL]<<"\n";
}
printOptimalAlignment(arr,y,x,yL-1,xL);
}
//really weird ((yL>1 && xL>1) && (((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1))
//not true if it is?
bool seperate = (((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1);
if(yL>=1 && xL>=1){
if((((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1) && (true)){
if(printinfo){
cout<<"-> Insert, current distance = "<<arr[yL][xL]<<"\n";
cout<<"Next Location1: "<<"("<<xL-1<<","<<yL<<")"<<"\n";
}
printOptimalAlignment(arr,y,x,yL,xL-1);
return;
//how does it get here... also return gets ignored... prob another stack issue
cout<<"insert function broke?????? # (1,1) ???????????????\n";
//return;
}
}
return;
cout<<"END... Hopefully.. if you see this Something went wrong\n";
because_Im_a_terrible_person:
cout<<"QUIT\n";
}
I suspect your problem is that your function calls itself and you don't appear to be taking into account what should happen next after that call to itself finishes. So you get to your finish condition where you say the return doesn't work, but it does... it just returns to where you left off in the previous call to printOptimalAlignment, which still might do something before returning to its caller, and so on. You have three different sites where you recursively call printOptimalAlignment that aren't immediately followed by a return statement, and at any of these it might be that the code will continue and trigger another of your conditional blocks.

Moving Boolean Values to Another Function C++

I need to move the value isAMFut and isAM into the function computeDifference()
cout<<"Is it AM or PM?"<<endl;
cin>>amOrPMFut;
transform(amOrPMFut.begin(), amOrPMFut.end(), amOrPMFut.begin(), ::toupper);
//Checking if user enters am or pm
while ((amOrPMFut != "AM") && (amOrPMFut != "PM")){
cout<<"Is it AM or PM?"<<endl;
cin>>amOrPMFut;
transform(amOrPMFut.begin(), amOrPMFut.end(), amOrPMFut.begin(), ::toupper);
}
if (amOrPMFut == "AM"){
isAMFut = true;
}
else {
isAMFut = false;
}
I would like to accomplish this by using pointers if that is possible. Otherwise I was thinking I could create the two booleans as global scope variables, but I don't feel like that is the most practical. Any insight would be much appreciated, please let me know if I can clear anything up.
Thanks!
int computeDifference(int& hours, int& minutes, int& hourFut, int& minFut){
//Calculations
//NEED TO TRANSFER VALUES OF BOOLEANS FROM MAIN FUNCTION INTO THIS FUNCTION
if ((isAM == true) && (isAMFut == true) {
if (hours > hourFut) {
diffHr = abs((hours - hourFut - 24));
diffMin = abs((minutes - minFut));
}
}
}

Keeps on getting terminate called after throwing an instance of 'std::out_of_range'

bool GameUtil::isValidPath(std::vector<int>& path, Player* player, Game* game) {
///**Get borad*/
std::vector<Square*> board = game->getBoard();
int maxDistanceTravel = 0;
int playerCanTravel = 0;
//first square and last square must be present
if (path[0] == 0 && path[path.size() - 1] == (board.size() - 1)) {
for (int i = 0; i < path.size() - 1; i++) {
/**Max distance of each board from player*/
maxDistanceTravel = compute(board.at(path[i]), player);
playerCanTravel = path[i + 1] - path[i];
if ((playerCanTravel > maxDistanceTravel) && (playerCanTravel <= 0)) {
return false;
}
}
return true;
}
return false;
}
I am a new c++ learner and I am getting the same error over and over again but could not figure out what is wrong with it, it is not obviously an out of range, please help, thanks.
I would first try doing some early checking to make sure you're inputs are valid. Perhaps something like:
bool GameUtil::isValidPath(std::vector<int>& path, Player* player, Game* game) {
assert(!path.empty());
if (path.size() < 2) return false; // or whatever your minimum size is.
As long as you're at it, you should probably scrub the other inputs.
bool GameUtil::isValidPath(std::vector<int>& path, Player* player, Game* game) {
assert(!path.empty());
assert(game);
assert(player);
if (path.size() < 2) return false; // or whatever your minimum size is.
I have a rule of thumb to use references for arguments that should never be null. It looks like in your code that game isn't allowed to be null. So perhaps you could change the function:
bool GameUtil::isValidPath(std::vector<int>& path, Player& player, Game& game) {
assert(!path.empty());
if (path.size() < 2) return false; // or whatever your minimum size is.
And then change where you call. So probably in your code you have something like:
// somewhere buried in your code:
Game * myGame = .../// however you created it.
Game * myPlayer = .../// however you created it.
GameUtil * myGameUtil = .../// however you created it.
...
auto isValid = myGameUtil->isValidPath(path, myPlayer, myGame);
You change it to:
// somewhere buried in your code:
Game * myGame = .../// however you created it.
Game * myPlayer = .../// however you created it.
GameUtil * myGameUtil = .../// however you created it.
...
auto isValid = myGameUtil->isValidPath(path, *myPlayer, *myGame);
Hope that helps.

strange segmentation fault during function return

I am running a program on 2 different machines. On one it works fine without issue. On the other it results in a segmentation fault. Through debugging, I have figured out where the fault occurs, but I can't figure out a logical reason for it to happen.
In one function I have the following code:
pass_particles(particle_grid, particle_properties, input_data, coll_eros_track, collision_number_part, world, grid_rank_lookup, grid_locations);
cout<<"done passing particles"<<endl;
The function pass_particles looks like:
void pass_particles(map<int,map<int,Particle> > & particle_grid, std::vector<Particle_props> & particle_properties, User_input& input_data, data_tracking & coll_eros_track, vector<int> & collision_number_part, mpi::communicator & world, std::map<int,int> & grid_rank_lookup, map<int,std::vector<double> > & grid_locations)
{
//cout<<"east-west"<<endl;
//east-west exchange (x direction)
map<int, vector<Particle> > particles_to_be_sent_east;
map<int, vector<Particle> > particles_to_be_sent_west;
vector<Particle> particles_received_east;
vector<Particle> particles_received_west;
int counter_x_sent=0;
int counter_x_received=0;
for(grid_iter=particle_grid.begin();grid_iter!=particle_grid.end();grid_iter++)
{
map<int,Particle>::iterator part_iter;
for (part_iter=grid_iter->second.begin();part_iter!=grid_iter->second.end();)
{
if (particle_properties[part_iter->second.global_part_num()].particle_in_box()[grid_iter->first])
{
//decide if a particle has left the box...need to consider whether particle was already outside the box
if ((part_iter->second.position().x()<(grid_locations[grid_iter->first][0]) && part_iter->second.position().x()>(grid_locations[grid_iter->first-input_data.z_numboxes()][0]))
|| (input_data.periodic_walls_x() && (grid_iter->first-floor(grid_iter->first/(input_data.xz_numboxes()))*input_data.xz_numboxes()<input_data.z_numboxes()) && (part_iter->second.position().x()>(grid_locations[input_data.total_boxes()-1][0]))))
{
particles_to_be_sent_west[grid_iter->first].push_back(part_iter->second);
particle_properties[particle_grid[grid_iter->first][part_iter->first].global_part_num()].particle_in_box()[grid_iter->first]=false;
counter_sent++;
counter_x_sent++;
}
else if ((part_iter->second.position().x()>(grid_locations[grid_iter->first][1]) && part_iter->second.position().x()<(grid_locations[grid_iter->first+input_data.z_numboxes()][1]))
|| (input_data.periodic_walls_x() && (grid_iter->first-floor(grid_iter->first/(input_data.xz_numboxes()))*input_data.xz_numboxes())>input_data.xz_numboxes()-input_data.z_numboxes()-1) && (part_iter->second.position().x()<(grid_locations[0][1])))
{
particles_to_be_sent_east[grid_iter->first].push_back(part_iter->second);
particle_properties[particle_grid[grid_iter->first][part_iter->first].global_part_num()].particle_in_box()[grid_iter->first]=false;
counter_sent++;
counter_x_sent++;
}
//select particles in overlap areas to send to neighboring cells
else if ((part_iter->second.position().x()>(grid_locations[grid_iter->first][0]) && part_iter->second.position().x()<(grid_locations[grid_iter->first][0]+input_data.diam_large())))
{
particles_to_be_sent_west[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
else if ((part_iter->second.position().x()<(grid_locations[grid_iter->first][1]) && part_iter->second.position().x()>(grid_locations[grid_iter->first][1]-input_data.diam_large())))
{
particles_to_be_sent_east[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
++part_iter;
}
else if (particles_received_current[grid_iter->first].find(part_iter->first)!=particles_received_current[grid_iter->first].end())
{
if ((part_iter->second.position().x()>(grid_locations[grid_iter->first][0]) && part_iter->second.position().x()<(grid_locations[grid_iter->first][0]+input_data.diam_large())))
{
particles_to_be_sent_west[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
else if ((part_iter->second.position().x()<(grid_locations[grid_iter->first][1]) && part_iter->second.position().x()>(grid_locations[grid_iter->first][1]-input_data.diam_large())))
{
particles_to_be_sent_east[grid_iter->first].push_back(part_iter->second);
counter_sent++;
counter_x_sent++;
}
part_iter++;
}
else
{
particle_grid[grid_iter->first].erase(part_iter++);
counter_removed++;
}
}
}
world.barrier();
mpi::request reqs_x_send[particles_to_be_sent_west.size()+particles_to_be_sent_east.size()];
vector<multimap<int,int> > box_sent_x_info;
box_sent_x_info.resize(world.size());
vector<multimap<int,int> > box_received_x_info;
box_received_x_info.resize(world.size());
int counter_x_reqs=0;
//send particles
for(grid_iter_vec=particles_to_be_sent_west.begin();grid_iter_vec!=particles_to_be_sent_west.end();grid_iter_vec++)
{
if (grid_iter_vec->second.size()!=0)
{
//send a particle. 50 will be "west" tag
if (input_data.periodic_walls_x() && (grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes()))*input_data.xz_numboxes()<input_data.z_numboxes()))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1)], grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1), particles_to_be_sent_west[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1)]].insert(pair<int,int>(world.rank(), grid_iter_vec->first + input_data.z_numboxes()*(input_data.x_numboxes()-1)));
}
else if (!(grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes()))*input_data.xz_numboxes()<input_data.z_numboxes()))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()], grid_iter_vec->first - input_data.z_numboxes(), particles_to_be_sent_west[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()]].insert(pair<int,int>(world.rank(),grid_iter_vec->first - input_data.z_numboxes()));
}
}
}
for(grid_iter_vec=particles_to_be_sent_east.begin();grid_iter_vec!=particles_to_be_sent_east.end();grid_iter_vec++)
{
if (grid_iter_vec->second.size()!=0)
{
//send a particle. 60 will be "east" tag
if (input_data.periodic_walls_x() && (grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes())*input_data.xz_numboxes())>input_data.xz_numboxes()-input_data.z_numboxes()-1))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1)], 2000000000-(grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1)), particles_to_be_sent_east[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1)]].insert(pair<int,int>(world.rank(),2000000000-(grid_iter_vec->first - input_data.z_numboxes()*(input_data.x_numboxes()-1))));
}
else if (!(grid_iter_vec->first-floor(grid_iter_vec->first/(input_data.xz_numboxes())*input_data.xz_numboxes())>input_data.xz_numboxes()-input_data.z_numboxes()-1))
{
reqs_x_send[counter_x_reqs++]=world.isend(grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()], 2000000000-(grid_iter_vec->first + input_data.z_numboxes()), particles_to_be_sent_east[grid_iter_vec->first]);
box_sent_x_info[grid_rank_lookup[grid_iter_vec->first + input_data.z_numboxes()]].insert(pair<int,int>(world.rank(), 2000000000-(grid_iter_vec->first + input_data.z_numboxes())));
}
}
}
counter=0;
for (int i=0;i<world.size();i++)
{
//if (world.rank()!=i)
//{
reqs[counter++]=world.isend(i,1000000000,box_sent_x_info[i]);
reqs[counter++]=world.irecv(i,1000000000,box_received_x_info[i]);
//}
}
mpi::wait_all(reqs, reqs + world.size()*2);
//receive particles
//receive west particles
for (int j=0;j<world.size();j++)
{
multimap<int,int>::iterator received_info_iter;
for (received_info_iter=box_received_x_info[j].begin();received_info_iter!=box_received_x_info[j].end();received_info_iter++)
{
//receive the message
if (received_info_iter->second<1000000000)
{
//receive the message
world.recv(received_info_iter->first,received_info_iter->second,particles_received_west);
//loop through all the received particles and add them to the particle_grid for this processor
for (unsigned int i=0;i<particles_received_west.size();i++)
{
particle_grid[received_info_iter->second].insert(pair<int,Particle>(particles_received_west[i].global_part_num(),particles_received_west[i]));
if(particles_received_west[i].position().x()>grid_locations[received_info_iter->second][0] && particles_received_west[i].position().x()<grid_locations[received_info_iter->second][1])
{
particle_properties[particles_received_west[i].global_part_num()].particle_in_box()[received_info_iter->second]=true;
}
counter_received++;
counter_x_received++;
}
}
else
{
//receive the message
world.recv(received_info_iter->first,received_info_iter->second,particles_received_east);
//loop through all the received particles and add them to the particle_grid for this processor
for (unsigned int i=0;i<particles_received_east.size();i++)
{
particle_grid[2000000000-received_info_iter->second].insert(pair<int,Particle>(particles_received_east[i].global_part_num(),particles_received_east[i]));
if(particles_received_east[i].position().x()>grid_locations[2000000000-received_info_iter->second][0] && particles_received_east[i].position().x()<grid_locations[2000000000-received_info_iter->second][1])
{
particle_properties[particles_received_east[i].global_part_num()].particle_in_box()[2000000000-received_info_iter->second]=true;
}
counter_received++;
counter_x_received++;
}
}
}
}
mpi::wait_all(reqs_y_send, reqs_y_send + particles_to_be_sent_bottom.size()+particles_to_be_sent_top.size());
mpi::wait_all(reqs_z_send, reqs_z_send + particles_to_be_sent_south.size()+particles_to_be_sent_north.size());
mpi::wait_all(reqs_x_send, reqs_x_send + particles_to_be_sent_west.size()+particles_to_be_sent_east.size());
cout<<"x sent "<<counter_x_sent<<" and received "<<counter_x_received<<" from rank "<<world.rank()<<endl;
cout<<"rank "<<world.rank()<<" sent "<<counter_sent<<" and received "<<counter_received<<" and removed "<<counter_removed<<endl;
cout<<"done passing"<<endl;
}
I only posted some of the code (so ignore the fact that some variables may appear to be undefined, as they are in a portion of the code I didn't post)
When I run the code (on the machine in which it fails), I get done passing but not done passing particles
I am lost as to what could possibly cause a segmentation fault between the end of the called function and the next line in the calling function and why it would happen on one machine and not another.
If you're crashing between the end of a function and the subsequent line in the caller, you're probably crashing in the destructor of a local variable. You need to run the program in a debugger to find out which object's destructor is crashing.
There are a couple of possibilities:
You actually are returning, but cout is buffered by the OS so you don't see "done passing particles" because the application crashes first.
You have some local class that has a destructor that is seg faulting.
Try running it in a debugger to find out where it is actually crashing.
Edit:
Since you've mentioned you're using gcc, add the -g flag and run it with gdb. Gdb will then tell you exactly where it's going wrong (probably a null dereference).
Just in case anyone comes back to this later. I updated to the newest version of boost mpi(at the time), 1.50 and this issue went away. Not much of a solution, but it worked.