Skip to content

C++ OOP segmentation fault

An answer to this question on Stack Overflow.

Question

I have a task at the college. I'm making a chess board with chess pieces on it. I have a method step() to move them. But I need to print the result of the move. I get a segmentation fault after trying to print it. And are there any other methods to declare that chess board with symbols. It seems to me it is not properly declared. (Yes, chess pieces should be like "01""10". I will use that later to make rules for each piece type).

#include <iostream>
#include <stdio.h>
#include <string>
#define n 8
using namespace std;
int convert(char c) {
  if(c=='a')
    return 0;
  else if(c=='b')
    return 1;
  else if(c=='c')
    return 2;
  else if(c=='d')
    return 3;
  else if(c=='e')
    return 4;
  else if(c=='f')
    return 5;
  else if(c=='g')
    return 6;
  else 
    return 7;
}
class chess {
private:
  string board[n][n]= {{"02","03","04","06","05","04","03","02"}, //chessboard
                      {"01","01","01","01","01","01","01","01"}, //black
                      {"00","00","00","00","00","00","00","00"},
                      {"00","00","00","00","00","00","00","00"},
                      {"00","00","00","00","00","00","00","00"}, //00-empty cells
                      {"00","00","00","00","00","00","00","00"},
                      {"11","11","11","11","11","11","11","11"}, //white
                      {"12","13","14","16","15","14","13","12"}};
  public:
  chess() {}
  void print() {
    cout<<"  _";  //board marking
    for (int i='a'; i<='h'; i++)
      printf("_%c_",i);
    
    cout<<endl;
    for (int i=0; i<n; i++) { 
      printf("%d| ", i+1); //board marking
      for (int j=0; j<n; j++) {
        printf("%.2s ",board[i][j].c_str()); //print chess pieces
      }
      cout<<'\n';
    }
    cout<<'\n';
  }
  void step() { //move on another cell
    char figc, fign, cellc, celln;
    cout<<"Piece, char: "; //choose a chess piece
    cin>>figc;
    cout<<"Piece, num: ";
    cin>>fign;
    cout<<"Step, char: "; //move on cell - ...
    cin>>cellc;
    cout<<"Step, num: ";
    cin>>celln;
    board[celln-1][convert(celln)]=board[fign-1][convert(fign)];
    board[fign-1][convert(fign)]="00";
  }
};
int main() {
  chess A;
  A.step();
  A.print();
  return 0;
}

Example. Screenshot

Answer

Compiling your code with

g++ -g -faddress=sanitize main.cpp

Shows the following output:

#0 0x7fdc8e7c4b40 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x124b40)
    #1 0x7fdc8e7c4f18 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x124f18)
    #2 0x56310c7ad092 in chess::step() /z/bob.cpp:65
    #3 0x56310c7a9c10 in main /z/bob.cpp:72
    #4 0x7fdc8e0b8b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #5 0x56310c7a9a19 in _start (/z/a.out+0x1a19)

Line 65 is:

board[fign-1][convert(fign)]="00";

in which we see fign, which is a char used as an integer.

Since the ascii range of letters and numbers is somewhere in the range 48-122, this exceeds the size of your array.

Using

board.at(fign-1).at(convert(fign))="00";

would have told you this immediately because .at() does bounds checking.