2012-05-08 16 views
1

私はC言語を使用してアプリケーションを作成しています。 私は構造に基づいた新しいタイプの作成:次にC言語で動的に割り当てられた構造体の配列を使用

typedef struct ENTITY 
{ 
    char * field1; 
    char * field2; 
} entity; 

を、私は動的エンティティの配列を割り当てる機能を定義しました。私のメインのファイルで

int my_function(entity ** my_array) 
{ 
    count = random_int(1, 10); 

    entity * result; 
    result = (entity *) calloc(count, sizeof(entity)); 

    int i; 
    for(i = 0 ; i < count ; i++) 
    { 
     (result+i)->field1 = strdup("Blabla in field1"); 
     (result+i)->field2 = strdup("Blabla in flied2"); 

     // This line print correctly "Blabla in field1" for each element in the array. 
     printf("->{%s}\n", (result+i)->field1); 
    } 

    *my_array = result; 
    return count; 
} 

、私はこの機能を使用します。

entity * my_array; 
count = my_function(&my_array); 

for(i = 0 ; i < count ; i++) 
{ 
    printf("field1 of the element %d: %s\n", i, my_array[i].field1); 
} 

私の配列が< = 3個の要素で満たされている場合、このコードは何らかの理由で動作します。配列内の4つの要素からセグメント化エラーが発生します:

->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
field1 of the element 0: `�fZ$ 
Segmentation fault 

私はここで動的割り当てについて多くを読んだことがありますが、この問題を修正することはできません。どんな手掛かり?

ありがとうございました!あなたのコードは、(random_intの呼び出しを削除した後に動作します

+1

は 'ない万一結果は 'エンティティ**'でしょうか? –

+2

いいえ、これは基本的にうまく動作します(欠落したヘッダーを追加し、カウント変数を宣言し、省略された 'random_int'関数を文字列' 4 'で置き換えると)。あなたは同じ(ハードコード 'int count = 4')を試して、まだ失敗するかどうか確認できますか?そうであれば、まだ問題を示している最小限の_compilable_コードを投稿してください。 – Useless

+2

@userあなたがやっていることに何も間違いはありません(http://ideone.com/V4jgG)。 'random_int'関数が何をしているのかを示します。そして、あなたが何をしているのか正確には見せていないかもしれません。 – Mahesh

答えて

1

)と、以下のようにハードコードされた金額に置き換える:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 


typedef struct ENTITY 
{ 
    char * field1; 
    char * field2; 
} entity; 


int my_function(entity ** my_array) 
{ 
    int count = 10; 

    entity * result; 
    result = (entity *) calloc(count, sizeof(entity)); 

    int i; 
    for(i = 0 ; i < count ; i++) 
    { 
     (result+i)->field1 = strdup("Blabla in field1"); 
     (result+i)->field2 = strdup("Blabla in flied2"); 
    } 

    for(i = 0 ; i < count ; i++) 
    { 
     // This line print correctly "Blabla in field1" for each element in the array. 
     printf("->{%s}\n", (result+i)->field1); 
    }  


    *my_array = result; 
    return count; 
} 



int main(int argc, char *argv[]) 
{ 
    int count; 
    int i; 

    entity * my_array; 
    count = my_function(&my_array); 

    for(i = 0 ; i < count ; i++) 
    { 
     printf("MAIN::field1 of the element %d: %s\n", i, my_array[i].field1); 
     printf("MAIN::field2 of the element %d: %s\n", i, my_array[i].field2); 
    } 

    return(0); 
} 

これの出力は次のとおりです。

[[email protected] SO]# gcc array.c 
[[email protected] SO]# ./a.out 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
->{Blabla in field1} 
MAIN::field1 of the element 0: Blabla in field1 
MAIN::field2 of the element 0: Blabla in flied2 
MAIN::field1 of the element 1: Blabla in field1 
MAIN::field2 of the element 1: Blabla in flied2 
MAIN::field1 of the element 2: Blabla in field1 
MAIN::field2 of the element 2: Blabla in flied2 
MAIN::field1 of the element 3: Blabla in field1 
MAIN::field2 of the element 3: Blabla in flied2 
MAIN::field1 of the element 4: Blabla in field1 
MAIN::field2 of the element 4: Blabla in flied2 
MAIN::field1 of the element 5: Blabla in field1 
MAIN::field2 of the element 5: Blabla in flied2 
MAIN::field1 of the element 6: Blabla in field1 
MAIN::field2 of the element 6: Blabla in flied2 
MAIN::field1 of the element 7: Blabla in field1 
MAIN::field2 of the element 7: Blabla in flied2 
MAIN::field1 of the element 8: Blabla in field1 
MAIN::field2 of the element 8: Blabla in flied2 
MAIN::field1 of the element 9: Blabla in field1 
MAIN::field2 of the element 9: Blabla in flied2 
関連する問題