1

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

我在这里阅读了很多关于动态分配的信息,但我无法解决这个问题。有什么线索吗?

谢谢你的帮助!

4

1 回答 1

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
于 2012-05-08T16:16:49.610 回答