私は "ユニオン検索"をしようとしています。ここで複雑な2D配列を割り当てることができません
は私のコードです:
UnionFind uf_create(){
UnionFind uf= malloc(sizeof(UnionFind));
uf->vt=malloc(11*sizeof(VertexTree*));
uf->nbElems=VERTEX_MAX;
uf->nbGroups=VERTEX_MAX;
int i;
for(i=0;i<uf->nbElems;i++){
printf("%d\n", i);
uf->vt[i]->vtx=i+1;
uf->vt[i]->parent=uf->vt[i];
}
return uf;
}
UnionFindがで定義されます。
typedef struct unionfind{
unsigned int nbElems;
unsigned int nbGroups;
VertexTree **vt;
}*UnionFind;
そして、ここではツリーの定義です:
typedef struct sTree{
GraphVertex vtx;
struct sTree* parent;
}VertexTree;
私はセグメンテーション違反がある知っていますツリーが正しく割り当てられていないためです。 頂点のツリーにメモリを正しく割り当てる方法を教えてもらえますか?
おかげ
- > vtx'未定義の動作を引き起こします。 'uf-> nbGroups = ...'の直後に各vt [i]を初期化するforループを書くべきです。 – ddz
これは私がやったことです、今、segfaultは割り当て行で起こっています( "uf-> vt [i] = malloc(sizeof(VertexTree));") – Elirovi
'for(i = 0; i <11; + + i){uf-> vt [i] = malloc(sizeof ** uf-> vt); } 'はあなたにセグメンテーションを与えていますか? – ddz