简体   繁体   中英

Trouble allocating memory in 2Darray of a structure

I am trying to develop the TicTacToe game in C and I am using the following structures.The myboard->a member is used to store 'X' or 'O' ,meaning the move a player has made in the same coordinates as the board , ie. 'X' in (1.1) or 'O' (3.1)

typedef struct{
  int** a;      
  int size;
}_board;
typedef _board* board;
board myboard =(board) malloc(sizeof(board));
scanf_s("%d", &(myboard->size));

Size=is the size of the TicTacToe board nxn.

myboard->a =(int**)malloc(myboard->size*sizeof(char *));
if (myboard->a = NULL)
{
    printf("ERROR!!!");
}

Until this point everything seems to work but when a try to allocate memory as you see bellow, a get a segmentation fault.

int i;
for (i = 0; j<myboard->size; i++)
{
    myboard->a[i] = malloc(sizeof(char));
    if (myboard->a[i] == NULL)
    {
        printf("ERROR");
    }
}

I am using free for its malloc at the end of me program. Thanks, in advance for any answer , and sorry for my bad english. Any help is apreciated.

I don't really understand why you are using a int** variable for storing your board data, when you could just use one simple int* , which you could later assign with malloc(3) as in:

int* b_data;
int  b_size = 9;

b_data = malloc(b_size * sizeof(int));
// ... Your code
free(b_data);

If however, you really want to use a int** variable, you could do something like:

int** b_data;
int   b_size_x = 3;
int   b_size_y = 3;
int   i;

b_data = malloc(b_size_x * sizeof(int*));
for(i = 0; i < b_size_x; i++)
    *(b_data + i) = malloc(b_size_y * sizeof(int));
// ... Your code where you access your array of size_x containing
// arrays of size_y of int typed "cells"
for(i = 0; i < b_size_x; i++)
    free(*(b_data + i));
free(b_data);

But this is really unnecessarily complicated, and I would dis-advise from doing so unless for learning purposes : in most cases, the allocated memory will be contiguous, so the second solution will allocate a nearly identical structure in memory, but will be less efficient (the "2D" array solution takes b_size_x times sizeof(int*) more memory), and the first solution will be simpler to read/write (so less prone to bugs, and easier to maintain).


Concerning your code, your problem is that myboard->a is NULL right after the "check" (which is in fact an allocation - note that the "ERROR" is never shown because myboard-> is then NULL, which evaluate to 'false'.):

if (myboard->a = NULL)

which should be:

if (myboard->a == NULL)

In addition, as pointed by @WhozCraig in his comment on your post, you might want to use i in the condition of your for loop, rather than j ; and you also probably want to use int and int* types rather than char and char* types respectively in your malloc(s), since the a member of the board structure is an int** . Oh and also, think about putting newlines after your printf(3) strings, or use puts(3) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM