0

After only creating a matrix and printing it out I get a segmentation error... All chars of my matrix are printed but after the last line I print:

std::cout << endl;

and I get the segmentation error.

My code:

Header:

class Board{

    private:
        struct coord {
            int x;
            int y;
        };
        coord _coord;

        char** board;
        int size;

    public:

        Board(int v);   
        //~Board();
        friend std::ostream& operator<<(std::ostream& os, Board const &b); 
};

My CPP code:

Board::Board(int v)
{
    size = v;
    board = new char* [size];

    for (int i=0; i<size; i++)
    {
        board[i] = new char[size];
        for(int j = 0 ; j < size ; j++){
            board[i][j] = '*';
        }
    }
}

ostream& operator<<(std::ostream& os, Board const &b)

    {
        for(int i = 0 ; i < b.size ; i++){
            for(int j = 0 ; j < b.size ; j++){
                cout << b.board[i][j] << " ";
            }
            cout << endl; // when (i == 3) the debug tells me after this I am thrown out
        }
        //cout << " "  << endl;

    }

My main:

#include "Board.h"
#include <iostream>
#include <vector>
//#include <map>
using namespace std;

int main() {
    Board board1{4};  // Initializes a 4x4 board
    cout << board1 << endl; 
    return 0;
}

Then I get:

* * * * 
* * * * 
* * * * 
* * * * 
Segmentation fault

but if I discomment: "//cout << " " << endl;" I don't have any more a segmentation error.

Where is the problem? it looks too simple but still, I am getting an error. (With the extra cout << " " << endl; line I am can continue and give in my assignment but I believe I should learn more and find out the problem)

I saw here that in whole in some situation that I am getting to an area in the memory that I am not supposed to get to, but that I know and I am asking on my specific code, that's why it is not a duplicate. Also, here there was a similar question but was specific and did not relate to my question.

Tomer
  • 531
  • 7
  • 19
  • if you're programming in C++, i suggest you use the STL container. Your code will be cleaner and less error prone – Clonk May 05 '18 at 23:30
  • Please post a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). – Jodocus May 05 '18 at 23:36
  • @Jodocus thanks for your input, I tried to edit more the question, hope it stands to the standards – Tomer May 05 '18 at 23:49
  • 1
    Provide some code that allows us to reproduce your problem. After that given that you are manipulaating arrray of pointers in class constructors, I'd say your problem is that the operator function try to acess dereferenced memory (hence the segfault). I strongly advise creating implementing a matrix class using std::vector – Clonk May 05 '18 at 23:52

1 Answers1

2

Does that even compile? You are missing a return statement from your operator<< overload. Your implementation is also wrong, you should be using the ostream passed to the function for printing rather than using cout directly and then return it :

    friend ostream& operator<<(std::ostream& os, Board const &b)
    {
        for (int i = 0; i < b.size; i++) {
            for (int j = 0; j < b.size; j++) {
                os << b.board[i][j] << " ";
            }
            os << endl; // when (i == 3) the debug tells me after this I am thrown out
        }

        os << " "  << endl;
        return os;
    }

cout is one of the available ostream objects (there is also cerr and clog) and you want your operator to support all of them. With that being said, you should be using STL containers instead of using raw pointers.

A. Laribi
  • 106
  • 3
  • 1
    Thanks! Yes, it compiles since doing cout<< is allowed at the end it is only a func. also for some reason cpp is loose about the idea of returning types or returning at all. Again thanks for the answer now I understand. – Tomer May 06 '18 at 00:04
  • 1
    "Does that even compile?" It probably compiles, and if you're lucky the compiler will issue a warning. This burned me a couple of months ago; the program even worked with `-O0` and crashed only when I introduced some optimization. – John Perry May 06 '18 at 00:20
  • VC++ does issue a warning of level1 and it is automatically promoted to an error. Turning off all warnings (/W0) doesn't get rid of the error either. – A. Laribi May 06 '18 at 00:35