Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Segmentation Fault – malloc 2D array

Trying to create a map for a little game. When initialising the map with 2D arrays using malloc, the main function will run okay when the printMap function is commented out, however when trying to display the map with printMap, it returns a Segmentation fault. Totally lost at why this isn’t working. Any help appreciated.

This is work for University, who insist the code is in C89 and I compile with -ansi -pedantic -Wall -Werror.

GAME.C file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"random.h"

void createMap(char*** map, int xCoord, int yCoord) {
    int i, j;
    xCoord += 2;
    yCoord += 2;
    char** mapArray;

    mapArray = (char**)malloc(yCoord * sizeof(char*));
    for (i = 0; i < yCoord; i++) {
        mapArray[i] = (char*)malloc(xCoord * sizeof(char));
    }
    for (i = 0; i < yCoord; i++) {
        for (j = 0; j < xCoord; j++) {
            mapArray[i][j] = "0";
        }
    }
    *map = mapArray;
}

void printMap(char** map, int xCoord, int yCoord) {
    xCoord += 2;
    yCoord += 2;
    printf("%d, %d", xCoord, yCoord);
    int i, j;
    
    for (i = 0; i < yCoord; i++) {
        for (j = 0; j < xCoord; i++) {
            printf("%d %d", i, j);
            printf("%c", map[i][j]);
        }
        printf("\n");
    }
}

MAIN.C file

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "random.h"
#include "game.h"

int main(void) {
    int xCoord = 5;
    int yCoord = 5;

    char** map;

    createMap(&map, xCoord, yCoord);
    
    printMap(map, xCoord, yCoord);
    return 0;

}

>Solution :

The function createMap is incorrectly initializing objects of the type char with pointers of the type char * to which the string literal "0" is implicitly converted in these for loops

for (i = 0; i < yCoord; i++) {
    for (j = 0; j < xCoord; j++) {
        mapArray[i][j] = "0";
    }
}

Instead of the string literal you need to use integer character constant '0' as for example

for (i = 0; i < yCoord; i++) {
    for (j = 0; j < xCoord; j++) {
        mapArray[i][j] = '0';
    }
}

Another problem is a typo in this loop within the function printMap

for (j = 0; j < xCoord; i++) {
                        ^^^^

You need to write

for (j = 0; j < xCoord; j++) {
                        ^^^^
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading