Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 访问结构数组时发生内存冲突_C_Arrays_Struct - Fatal编程技术网

C 访问结构数组时发生内存冲突

C 访问结构数组时发生内存冲突,c,arrays,struct,C,Arrays,Struct,我正处于指针学习曲线上,确实需要一些指导/帮助。我希望有一个结构数组,每个结构都是一个“单元”,用于跟踪各种事物。一切似乎都很好,没有编译错误或任何事情,我正在使用数组生成一个映射。当我尝试在不同点访问阵列时,就会出现问题。有时我会遇到内存访问冲突,有时我不会——这意味着我很幸运。我是C语言的新手,所以任何帮助都是非常感谢的——或者说是指向正确的方向。我真的很想知道我为什么出错,哪里出错,我觉得这是我的指针和记忆——我传递的东西正确吗?先谢谢你 #define ysize 20 #define

我正处于指针学习曲线上,确实需要一些指导/帮助。我希望有一个结构数组,每个结构都是一个“单元”,用于跟踪各种事物。一切似乎都很好,没有编译错误或任何事情,我正在使用数组生成一个映射。当我尝试在不同点访问阵列时,就会出现问题。有时我会遇到内存访问冲突,有时我不会——这意味着我很幸运。我是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

由于发布的代码主要起作用,可能错误在其他地方。可能你的指数超出了范围

#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);
}
#包括
#包括
#第20页
#定义XSIZE80
类型定义结构单元{
int型;
布尔访问;
差强人意;
国际项目;
}细胞;
int getCell(int x,int y,结构单元*map)
{
返回映射[x+xsize*y]。类型;
}
void setCell(int x,int y,int celltype,struct cells*map)
{
map[x+xsize*y].type=celltype;
}
结构单元*makeMap()
{
结构单元*map=(单元*)malloc(xsize*ysize*sizeof(结构单元));
for(int i=0;i!=xsize*ysize;i++){
map[i].type=i;
映射[i]。项=0;
map[i].passable=true;
map[i].visted=false;
}
返回图;
}
int main()
{
结构单元*map=makeMap();
//getRand在提供的最小值和最大值之间生成一个随机数。
对于(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){
//映射[x+xsize*y]->项=3;
printf(“%d在[%d,%d]\n”,getCell(x,y,map),x,y);
}
//这就是代码失败的地方。
//有时它可以工作,但有时它会产生内存错误
}
免费(地图);
}

x,y的值是什么:post a.
void main()
int main()
你似乎很困惑,你是想要一个一维映射,从它的坐标计算
结构的索引,还是想要一个二维映射,其中你有一个指向
结构的其他一维数组的一维指针数组。在第一种情况下,您不需要双星(除非您希望通过函数参数而不是函数返回值来设置指针)。为什么需要一个指针数组?我可以看出我现在做错了什么,非常感谢。我似乎混淆了我的数组类型。很抱歉没有包括我所有的代码-我会记得下次发布时让它可以编译。我一直在使用“艰苦学习C”网站学习C语言,我想我需要对数组和指针进行更多的挖掘,以使它们在我的脑海中清晰可见。这很好用。非常感谢你。