简体   繁体   中英

Memory violation accessing an array of structs

i'm on the pointer learning curve and could really use some direction / assistance. I want to have an array of structs, with each struct being a 'cell' that keeps track of various things. Everything seems to work fine, no compiling errors or anything and i'm using the array to generate a map. The issue comes when i try and access the array at various points. Sometimes i get a memory access violation, sometimes i don't - which mean's i'm getting lucky. I'm very new to C so any help is appreciated - or pointing in the right direction. I really want to understand why and where I've gone wrong, and i get the feeling it's my pointers and memory - am i passing things correctly? Thank in advance.

#define ysize 20
#define xsize 80

typedef struct cells {
    int type;
    bool visited;
    bool passable;
    int item;
} cells;

int getCell(int x, int y, struct cells **map)
{
    return map[x + xsize * y]->type;
}
void setCell(int x, int y, int celltype, struct cells **map)
{
    map[x + xsize * y]->type = celltype;
}
struct cells **makeMap()
{
    struct cells **map = malloc(xsize * ysize * sizeof(struct cells *));
    for (int i = 0; i != xsize * ysize; i++) {
        map[i] = malloc(sizeof(struct cells ));
        map[i]->type = 0;
        map[i]->item = 0;
        map[i]->passable = true;
        map[i]->visited = false;
    }
    return map;
}


void main()
{
    struct cells ** map = makeMap();
    //getRand generates a random number between the min and max supplied.
    int x = getRand(0, xsize);
    int y = getRand(0, ysize);

    if (getCell(x, y, map) == tileCorridor || getCell(x, y, map) == tileDirtFloor){
        //map[x + xsize * y]->item = 3;
        //printf("%d", getCell(x, y, map));
    }
    // this is where the code is failing. 
    //sometimes it works, others it generates a memory error

    destroyMap(map);
}

Since you are doing the index calculation into a one dimensional you don't need to a two dimensional array. Here is a fuctional version of your code. I've improvised getRand and removed destroyMap both of which were still missing and added the includes.

Since the posted code mainly worked, maybe the error was elsewhere. Possibly your indices were out of bounds.

#include <malloc.h>
#include <stdlib.h>

#define ysize 20
#define xsize 80

typedef struct cells {
    int type;
    bool visited;
    bool passable;
    int item;
} cells;

int getCell(int x, int y, struct cells *map)
{
    return map[x + xsize * y].type;
}
void setCell(int x, int y, int celltype, struct cells*map)
{
    map[x + xsize * y].type = celltype;
}
struct cells *makeMap()
{
    struct cells *map = (cells*) malloc(xsize * ysize * sizeof(struct cells));
    for (int i = 0; i != xsize * ysize; i++) {
        map[i].type = i;
        map[i].item = 0;
        map[i].passable = true;
        map[i].visited = false;
    }
    return map;
}


int main()
{
    struct cells * map = makeMap();
    //getRand generates a random number between the min and max supplied.

    for( int i = 0; i < 10000; ++i)
    {
        int x = rand() % xsize;
        int y = rand() % ysize;

        int tileCorridor = 21;
        int tileDirtFloor = 143;


        if (getCell(x, y, map) == tileCorridor || getCell(x, y, map) == tileDirtFloor){
            //map[x + xsize * y]->item = 3;
            printf("%d at [%d, %d] \n", getCell(x, y, map), x , y);
        }
        // this is where the code is failing. 
        //sometimes it works, others it generates a memory error
    }
    free(map);
}

Live on Coliru

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