4

So I have what I think is a noob question. Sorry for that in advance, and for the grammar as english is not my primary language.

So I have to make a game of checkers. I have some struct, defined by

struct game {
int **board;
int xsize, ysize;

struct move *moves;
int cur_player;
};

struct coord {
int x, y;
};

struct move_seq {
struct move_seq *next;
struct coord c_old;
struct coord c_new; 
int piece_value;
struct coord piece_taken;
int old_orig;
};

struct move {
struct move *next;
struct move_seq *seq;
};

And I have to initialize a struct game wih the fonction

struct game *new_game(int xsize, int ysize)

So, here's my problem. I call, for now, new_game always with 10 and 10 values for xsize an ysize. Then I initialize the board game, which I want to assign later.

int black = 1;
int white = 5;
int **board;
int i;
int j;
int k;
for(i=0;i<xsize;i++)
{
    for(j=0;j<ysize;j++)
    {
        if(i<(xsize/2) && j<(ysize/2) && (i+j)%2!=0)
        {
            board[i][j] = black;
        }
        else if(i>(xsize/2) && j>(ysize/2) && (i+j)%2!=0)
        {
            board[i][j] = white;
        }
        else board[i][j] = 0;
    }
}
struct game *new = malloc (sizeof(struct game *));
if (new == NULL) return NULL;

So, my problem is after that. I just have Segmentation Fault whatever I do with my struct new.

I tried to do assign new->xsize = xsize and the same with ysize. I do a malloc for the board and the struct move, like I learned to do, but I kept getting this error of Segmentation Fault.

So here's my real question: How to assign and initialize correctly a struct ? Do I Have to make a malloc for each of the member of struct game ? (I tried that too but without any success...)

I don't necessarily want just the answer, I'd prefer to really understand what I have to do in this case and in general, to make less mistakes in the future.

Thanks in advance for your help.

Have a good day.

2
  • 1
    int **board; This pointer to pointer is never initialised; it could point to any pointer to anywhere. Commented Mar 7, 2015 at 15:40
  • 'new' is a reserved word in any compiler that is able to process C++ code, even though the code is C. Therefore, strongly suggest changing the name from 'new' to something more meaningful that is not a reserved word Commented Mar 7, 2015 at 16:48

2 Answers 2

7

It happens because you only allocated space for a pointer to your struct. What you need to do, is allocate it for the entire size of it:

struct game *new = malloc (sizeof(struct game));

Edit: Don't be mislead by the return value of malloc, as it returns a pointer to the allocated space, that's why it should be struct game *new as it is.

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to Michael's bug fix, I also wonder about board: it is not allocated, yet you write to it. I think it should be like:

struct game *new_game(int xsize, int ysize)
{
    int black = 1;
    int white = 5;
    int i;
    int j;

    struct game *new = malloc (sizeof(struct game));
    if (new == NULL) return NULL;
    game->xsize= xsize;
    game->ysize= ysize;
    game->board= malloc(xsize*ysize*sizeof(int));

    for(i=0;i<xsize;i++)
    {
        for(j=0;j<ysize;j++)
        {
            if(i<(xsize/2) && j<(ysize/2) && (i+j)%2!=0)
            {
                game->board[i*xsize+j] = black;
            }
            else if(i>(xsize/2) && j>(ysize/2) && (i+j)%2!=0)
            {
                game->board[i*xsize+j] = white;
            }
            else game->board[i*xsize+j] = 0;
        }
    }
    return (game);
}

Note also the array indexing: the compiler doesn't know the dynamic rowsize so you have to do that yourself.

5 Comments

When used at other places, I get a new Seg Fault with board. Is it correctly allocated the way you showed to me ? When I searched for how allocating memory for 2D array, I found other way. Like the way I did it with a loop for allocating memory for each line. And thanks again :)
My allocation of board and the board indexing are correct. When you do new->board = malloc(xsize*sizeof(int **)); you allocate room for one row of pointers (which happen to be the size of int) but you do not allocate the whole board. Just think: how many squares has the board? And allocate that much memory.
About the indexing: you allocated one chunk of linear memory and have to place the board in it, as rows (x) behind another. Then to adress the 3rd row, 2nd column, go 2*xsize right, then add 1 for the column (indexes start at zero). Note: indexing as board[i][j] only works if the compiler knows the rowsize (which it doesn't).
board is a pointer to pointer so this line: 'game->board= malloc(xsizeysizesizeof(int));' is not correct. ' aworkable method would be board = malloc (ysize * sizeof(int*)) then, in a loop, for each row, board[row] = malloc( xsize * sizeof(int) ) of course checking each malloc for failure
Yes, will work too. May ersult in awkward addressing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.