Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,360 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,250 people online right now. Registration is fast and FREE... Join Now!




seg fault

 
Reply to this topicStart new topic

seg fault

link129115
post 31 Aug, 2008 - 09:43 AM
Post #1


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

I'm not sure why I'm getting a seg fault with this code. It happens in the main() file when I call this line:
mapobject.grid[i][j]->act(i, j);

I am trying to make the ants and doodlebugs move around the grid randomly up, right, down, or left. The ants.txt and dbs.txt contain coordinates in the grid.

CODE

//this is the header file
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Organism;
class Map
{  
  public:
    Map();
    void printGrid();
    Organism *grid[20][20];
};

class Organism
{
  public:
    virtual void print()=0;
    virtual int type()= 0;
    void move(int,int);
    virtual void act(int, int) = 0;
};

class Ant: public Organism
{
  public:
    int type(){return 0;}
    void print(){cout<<'O';}
    void act(int, int);
};

class Doodlebug: public Organism
{
  public:
    int type(){return 1;}
    void print(){cout<< 'X';}
    void act(int, int);
};


//this is the member function definition file
#include "organism.h"


void Map::gridMap()
{
  fstream fin;

  int next, r, c;
  for(int i = 0; i < 20; i++){
    for(int j = 0; j<20; j++){
     grid[i][j] = NULL;
    }
  }
  fin.open("ants.txt");
  if(fin.fail()){
    cout << "Input file opening failed.\n";
    exit(1);
  }
  while(fin>>next){
    r = next;
    fin>>next;
    c = next;
   Ant *ant = new Ant;
    grid[c][r] = ant;
  }
  fin.close();
  fin.open("dbs.txt");
  if(fin.fail()){
    cout << "Input file opening failed.\n";
    exit(1);
  }
  while(fin>>next){
    r = next;
    fin>>next;
    c = next;
    Doodlebug *db = new Doodlebug;
    grid[c][r] = db;
  }
  fin.close();
}


void Map::printGrid()
{
  for(int i = 19; i >= 0; i--){
    for(int j=0;j < 20; j++){
      if(grid[i][j] == NULL){
         cout << ' ';
      }
      else
        grid[i][j]->print();
    }
   cout<<endl;
  }
}



void Ant::act(int i, int j)
{
     move(i, j);
    
}


void Doodlebug::act(int i, int j)
{
        move(i, j);
    
}      


void Organism::move(int i, int j)
{

  Organism *org[20][20];
  Map temp;
  int direction = rand()%4;

  switch(direction){
     case 0:
            if(i > 0 && (temp.grid[i-1][j] == NULL)){
             delete temp.grid[i][j];
             temp.grid[i-1][j] = new Ant;
             temp.grid[i][j] = NULL;
           }
           break;
     case 1:
           if(j < 20 && temp.grid[i][j+1] == NULL){
             delete temp.grid[i][j];
             temp.grid[i][j+1] = new Ant;
             temp.grid[i][j] = NULL;
           }
           break;
     case 2:
           if(j < 20 && temp.grid[i+1][j] == NULL){
             delete temp.grid[i][j];
             temp.grid[i+1][j] = new Ant;
             temp.grid[i][j] = NULL;
           }
           break;
     case 3:
           if(i > 0 && temp.grid[i][j-1] == NULL){
             delete temp.grid[i][j];
             temp.grid[i][j-1] = new Ant;
             temp.grid[i][j] = NULL;
           }
           break;
     default:
           break;
     }
}

//this is the main() file
#include "organism.h"

int main()
{
    srand(1);
    Map mapobject;
    string input;
    int i, j;
    
    mapobject.gridMap();
    while(intput[0] != 'q'){
        mapobject.printGrid();
        for( i = 0; i < 20; i++){
            for( j = 0; j < 20; j++){
    
                mapobject.grid[i][j]->act(i, j);
    
            }
        }
    cout << ">";
    getline(cin,input);
}
}

User is offlineProfile CardPM

Go to the top of the page

kapax
post 31 Aug, 2008 - 09:55 AM
Post #2


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 39



Thanked 1 times
My Contributions


Please, fix your code so it would compile because now it doesn't because Map class doesn't have all required functions. I would be glad to help you if the program at least compiled.
User is offlineProfile CardPM

Go to the top of the page

link129115
post 31 Aug, 2008 - 10:48 AM
Post #3


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

QUOTE(kapax @ 31 Aug, 2008 - 10:55 AM) *

Please, fix your code so it would compile because now it doesn't because Map class doesn't have all required functions. I would be glad to help you if the program at least compiled.

sorry about that. I had been trying different things and forgot to switch it back. Here is the part the correct Map class. the Map() constructor should just be void gridMap() instead.
CODE
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


class Organism;
class Map
{  
  public:
    void gridMap();
    void printGrid();
    Organism *grid[20][20];
};

class Organism
{
  public:
    virtual void print()=0;
    virtual int type()= 0;
    void move(int,int);
    virtual void act(int, int) = 0;
};

class Ant: public Organism
{
  public:
    int type(){return 0;}
    void print(){cout<<'O';}
    void act(int, int);
};

class Doodlebug: public Organism
{
  public:
    int type(){return 1;}
    void print(){cout<< 'X';}
    void act(int, int);
};


User is offlineProfile CardPM

Go to the top of the page

KYA
post 31 Aug, 2008 - 12:01 PM
Post #4


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


Where's your destructor? It needs to delete the pointer created upon creation of an object.
User is offlineProfile CardPM

Go to the top of the page

link129115
post 31 Aug, 2008 - 12:23 PM
Post #5


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

I created a destructor that looks like this:
CODE
  
~Map(){
      for(int i = 0; i < 20; i++)
        delete [] grid[i];
      delete [] grid;
    }


but i still get a seg fault when i run the program.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 31 Aug, 2008 - 01:09 PM
Post #6


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


Instead of simply creating an object, I would allocate it with the new word

Map objectName = new Map // etc
User is offlineProfile CardPM

Go to the top of the page

link129115
post 31 Aug, 2008 - 01:35 PM
Post #7


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

In the main() file I changed the variable declaration to Map *mapobject = new Map like you suggested and I also changed the void gridMap() function into a Map() constructor but I still get a seg fault at the line:
mapobject->grid[i][j]->act(i, j);
So now my main() file looks like:
CODE

int main()
{
  srand(1);
  string input;
  Map *mapobject = new Map;
  char next;
  int i=0, j=0;

  while(input[0] != 'q'){
    mapobject->printGrid();

    for(i = 0; i < 20; i++){
      for(j = 0; j < 20; j++){

       mapobject->grid[i][j]->act(i, j);
      }
    }
}
}
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 31 Aug, 2008 - 03:33 PM
Post #8


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


Well, I would guess that mapobject->grid[i][j] is NULL. Perhaps you should check that it's a valid Organism before calling act?
User is offlineProfile CardPM

Go to the top of the page

link129115
post 31 Aug, 2008 - 05:09 PM
Post #9


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

Ok so now I've changed my code a bit. For the move function, which is called in act() I have changed the code to:
CODE


void Organism::move(int i, int j)
{
  Map temp;
  int direction = rand()%4;

  switch(direction){
     case 0:
           if(i > 0 && (temp.grid[i-1][j] == NULL)){
             //delete temp.grid[i][j];
             temp.grid[i-1][j] = temp.grid[i][j];
             temp.grid[i][j] = NULL;
           }

           break;
     case 1:
           if(j < 20 && temp.grid[i][j+1] == NULL){
             //delete temp.grid[i][j];
             temp.grid[i][j+1] = temp.grid[i][j];
             temp.grid[i][j] = NULL;
           }
           break;
     case 2:
           if(j < 20 && temp.grid[i+1][j] == NULL){
             //delete temp.grid[i][j];
             temp.grid[i+1][j] = temp.grid[i][j];
             temp.grid[i][j] = NULL;
          }

           break;
     case 3:
           if(i > 0 && temp.grid[i][j-1] == NULL){
            // delete temp.grid[i][j];
             temp.grid[i][j-1] = temp.grid[i][j];
             temp.grid[i][j] = NULL;
           }
           break;
     default:
           break;
     }
moved = true;
}


And in main() I now check if the index is null like this:
CODE

    for(i = 0;i<20;i++){
     for(j=0;j<20;j++){
       if(mapobject->grid[i][j] == NULL)
         continue;
       else
          mapobject->grid[i][j]->act(i, j);



But now I get this error:

*** glibc detected *** predator.out: free(): invalid pointer: 0xbfa4c3cc ***

and a whole bunch of other stuff like a memory map and then a core dump. Any ideas on how to fix this?
User is offlineProfile CardPM

Go to the top of the page

link129115
post 31 Aug, 2008 - 07:33 PM
Post #10


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

never mind I figured out the error but now I still can't make the objects move around the grid. Does anyone have tips on making this happen.
My code as of now for main() and the move() function, which is called in the act() function, is:
CODE


int main()
{
  srand(1);
  string input;
  Map *mapobject = new Map;
  int i=0, j=0;
//  mapobject->gridMap();

do{
  mapobject->printGrid();
  cout << ">";
  getline(cin, input);
  if(input[0] == 'q')
     exit(1);
  else{
    for(i = 0;i<20;i++){
     for(j=0;j<20;j++){
       if(mapobject->grid[i][j] == NULL)
         continue;
       else
         mapobject->grid[i][j]->act(i, j);
     }
    }
  }

}while(input[0] != 'q');
________________________________________

void Organism::move(int i, int j)
{
  Map *temp=new Map;
  int direction = rand()%4;

  switch(direction){
     case 0:
           if(i > 0 && (temp->grid[i-1][j] == NULL)){
          
             temp->grid[i-1][j] = temp->grid[i][j];
             temp->grid[i][j] = NULL;
           }

           break;
     case 1:
           if(j < 20 && temp->grid[i][j+1] == NULL){
          
             temp->grid[i][j+1] = temp->grid[i][j];
             temp->grid[i][j] = NULL;
           }
           break;
     case 2:
           if(i < 20 && temp->grid[i+1][j] == NULL){
          
             temp->grid[i+1][j] = temp->grid[i][j];
             temp->grid[i][j] = NULL;
          }

           break;
     case 3:
           if(j > 0 && temp->grid[i][j-1] == NULL){
          
             temp->grid[i][j-1] = temp->grid[i][j];
             temp->grid[i][j] = NULL;
           }
           break;
     default:
           break;
     }
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month