简体   繁体   English

内存冲突访问结构数组

[英]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. 我是C语言的新手,因此不胜感激-或指出正确的方向。 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. 我即兴getRand并删除了destroyMap ,这两个都仍然缺少,并添加了包含。

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 住在科利鲁

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

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