Why does this code give Segmentation fault?

Asked

Viewed 82 times

1

Whenever the first line of the map is filled in, a Segmentation fault, I wonder why.

void newmap(int x, int y , int players){

    int linha,coluna;
    char **map = (char **) malloc(sizeof(char*) * y);
    *map = (char *)malloc(sizeof(char)*x);

    for(linha=0; linha<x; linha++){
        for(coluna=0; coluna<y; coluna ++){

           if(linha == 0 || coluna == 0 || linha == (x-1) || coluna == (y-1)) {
               map[linha][coluna] = '*';
               printf("*");
            }
        }
        printf("\n");
    }

}
  • Choose an answer that answered your question, it helps the community a lot

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

You are not allocating each row, you are allocating the general and then a single row, to allocate each row would have to each of them within the loop of rows. I do not know if it gives the expected result because the question does not inform this, and the code does not give greater indications, who knows if I can be optimized, but it would be so:

#include <stdio.h>
#include <stdlib.h>

void newmap(int x, int y , int players) {
    char **map = malloc(sizeof(char *) * y);
    for (int i = 0; i < x; i++) {
        map[i] = malloc(x);
        for (int j = 0; j < y; j++) {
            if (i == 0 || j == 0 || i == x - 1 || j == y - 1) {
               map[i][j] = '*';
               printf("*");
            }
        }
        printf("\n");
    }
}

int main(void) {
    newmap(3, 3 , 3);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2

I think you want to do it this way:

    /* Alocação dinamica relativamente à parte da linha*/
    char **map = (char **) malloc((sizeof(char*) * x)+1);

    /* Alocação dinamica relativamente à parte da coluna*/ 
    for(linha=0; linha<x; linha++)
        map[linha] = (char *)malloc((sizeof(char)*y)+1);

    for(linha=0; linha<x; linha++)
    {
        for(coluna=0; coluna<y; coluna ++)
        {

            if(linha == 0 || coluna == 0 || linha == (x-1) || coluna == (y-1))
            {
                map[linha][coluna] = '*';
                printf("*");
            }
        }
        printf("\n");
    }

First you have to allocate memory for each line, then for each line you need to allocate memory to the column.

To make it a nicer code I could put the map[linha] =(char*)malloc((sizeof(char)*y)+1); inside the first is, getting a cleaner code

Don’t forget to do the free correctly, making a cycle to clean each column and only then give free to the map.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.