繁体   English   中英

内存冲突访问结构数组

[英]Memory violation accessing an array of structs

我在指针学习曲线上,可以真正使用一些指导/帮助。 我想要一个结构体数组,每个结构体都是一个“单元”,用于跟踪各种事物。 一切似乎都正常,没有编译错误或任何东西,我正在使用数组来生成地图。 当我尝试在各个点访问阵列时,就会出现问题。 有时我遇到内存访问冲突,有时却没有-这意味着我很幸运。 我是C语言的新手,因此不胜感激-或指出正确的方向。 我真的很想了解我为什么出错以及哪里出错了,我感到这是我的指针和记忆-我是否正确传递了东西? 预先感谢。

#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);
}

由于您是将索引计算成一维,因此不需要二维数组。 这是代码的功能版本。 我即兴getRand并删除了destroyMap ,这两个都仍然缺少,并添加了包含。

由于发布的代码主要起作用,因此错误可能在其他地方。 可能您的索引超出范围。

#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);
}

住在科利鲁

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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