2016-03-27 8 views
0

私はポインタを学習曲線上にあり、実際にはある方向/支援を使用することができます。私は構造体の配列を持っています。それぞれの構造体はさまざまなことを追跡する 'セル'です。すべてがうまくいくように見える、コンパイルエラーや何も私はマップを生成するために配列を使用しています。問題は、私がさまざまな時点でアレイにアクセスしようとするときに起こります。ときにはメモリアクセス違反が発生することもありますが、時には私はそうではありません。つまり、私は運が良かったということです。私は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); 
} 
+0

xの値は、Yものである:(http://stackoverflow.com/help/mcve)、最小完全、かつ検証例]投稿。 – Thomas

+0

'void main()' 'int main()' – stackptr

+0

座標から 'struct 'のインデックスを計算する1次元マップを使用するのか、2次元マップを使用するのか混乱するようです。 Dマップには、 'struct'の他の1次元配列へのポインタの1次元配列があります。最初のケースでは、関数の戻り値の代わりに関数の引数を使用してポインタを設定しない限り、2つの星は必要ありません。 –

答えて

0

インデックス計算を1次元にするので、2次元配列は必要ありません。ここにあなたのコードのfuctionalバージョンです。私は即時に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); 
} 

Live on Coliru

+0

私は今何が間違っていたのか分かります。本当にありがとうございます。私は私の配列型の間で混乱しているようです。すべての私のコードを含んでいないことに対する謝罪 - 次回私が投稿するときにコンパイルできるように覚えています。私はCのウェブサイトを使ってCを学んできました。「難しい方法を学んでください」 - 私は配列とポインタをもっと深く掘り下げて自分の頭の中でクリアする必要があると思います。これは完全に機能します。どうもありがとうございます。 –

関連する問題