Core dumped in print

Hello People, I have a problem with my code, I am trying to make a gamefield existing of 8x8 tiles. But now I have a segmentation fault (core dumped.

The fault is indside this file:


// file othellobord.cc
#include <iostream>
#include "othellobord.h"
using namespace std;



othellobord::~othellobord ( ) {
// TODO
}//
othellobord::othellobord(){
}

bordvakje::bordvakje ( ){
for (int i = 0; i < 8; i++){
buren[i] = NULL;
}
}
char leesoptie ( ) { // inleesfunctie voor invoer via toetsenbord
char toets = cin.get ( );
while ( toets == '\n' ) {
toets = cin.get ( );
}

return toets;
} // inleesfunctie

void othellobord::drukaf ( ) {

bouwbord(1);
cout << ingang->info << endl;
bordvakje* hulp = ingang;
while ( hulp != NULL ) {
cout << hulp->info << endl;

hulp = hulp->volgende;

}//while


}//Afdrukken

void othellobord::rits (bordvakje* boven, bordvakje* onder){
while((boven != NULL)&&(onder!=NULL)){

onder->buren[0] = boven;
onder->buren[1] = boven -> buren[2];
onder->buren[7] = boven -> buren[6];
boven->buren[4] = onder;
boven->buren[3] = onder -> buren[2];
boven->buren[5] = onder -> buren[6];
onder = onder -> buren[2];
boven = boven -> buren [2];


}
}


bordvakje* othellobord::maakrij (int n) {
bordvakje* ingang;
bordvakje* result;

ingang = new bordvakje;
result = ingang;

for (int i = 0; i < n; i++){
bordvakje* volgende;
volgende = new bordvakje;
volgende->buren[2] = ingang;
ingang->buren[6] = volgende;
ingang = volgende;

}

return result;
}

void othellobord::bouwbord (int n) {

bordvakje* boven;
bordvakje* onder;

for (int i = 0; i < n; i++){

onder = maakrij ( 8 );
rits(boven,onder);
boven = onder;
}


}//for




// TODO


// TODO




Connected with these two files:

1 // file hoofd.cc
#include <iostream>
#include "othellobord.h"
using namespace std;

int main ( ) {

othellobord Othellobord;
Othellobord.drukaf ( );

return 0;
}//main\

and

// file othellobord.h
class bordvakje {
public:
char kleur; // 7 0 1
bordvakje* buren[8]; // 6 2
bordvakje ( ); // 5 4 3
int info;
bordvakje* volgende;
};//bordvakje

class othellobord {
private:
bordvakje* ingang;


int hoogte, breedte;
void rits (bordvakje* boven, bordvakje* onder);
bordvakje* maakrij (int n);
bool aandebeurt();
int aantalstenen();

//bool menswit();
//bool menszwart();


// TODO
public:
othellobord ( );
~othellobord ( );
void bouwbord (int n );
void randomzet (char kl, int & i, int & j);
void menszet (char kl, bool & gedaan, int & i, int & j);
void computerzet (char kl, bool & gedaan, int & i, int & j);
void drukaf ( );
bool klaar ( );
int gewonnen (char & kl);
void doezet (int i, int j, char kl);
void magzet (int i, int j, char kl);
bool bordvol();
//verwijder();
//vervolgpotje();
//nieuwothellobord();
// TODO
};//othellobord


Looking forward to your advise ^^
1
2
3
4
5
6
7
8
9
10
void othellobord::bouwbord (int n) 
{   bordvakje* boven;
    bordvakje* onder;

    for (int i = 0; i < n; i++)
    {   onder = maakrij ( 8 );
        rits (boven,onder);
        boven = onder;
    }
}

line 7: boven is an uninitialized pointer (garbage) when it is passed to rits on the first iteration.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
What do u mean exactly with a uninitialized pointer(garbage)?
How should i fix this?Which values in rits(boven,onder)?
I need to make an 8 * 8 othelloboard with pointers and i cannot print the board
Last edited on
What do u mean exactly with a uninitialized pointer(garbage)?

Line 2: What exactly do you think the value of boven is when you enter the function?

Hint: Its not NULL. Also, what I said about boven also applies to your onder pointer also.

In rits, you check if the passed pointers are NULL, but simply declaring a pointer does not ensure it's value is NULL.

To initialize the pointers, simply give them a value:
2
3
    bordvakje* boven = NULL;
    bordvakje* onder = NULL;


Last edited on
Hey I fix this but i get still segmentation fault(core dumped).
How can you print a pointer matrix?
Topic archived. No new replies allowed.