使用在C中动态分配的struct数组

时间:2012-05-08 15:21:47

标签: c arrays dynamic struct allocation

我正在使用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);
}

出于某种原因,当我的数组由&lt; = 3个元素完成时,此代码可以工作,来自数组中的4个元素我得到了分段错误错误:

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

我已经在这里阅读了很多关于动态分配的内容,但我无法解决这个问题。任何线索?

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您删除对random_int()的调用并将其替换为硬编码量后,您的代码可以正常工作,如下所示:

#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 );
}

这个输出是:

[root@jrn SO]# gcc  array.c
[root@jrn 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