gpt4 book ai didi

c - 使用在 C 中动态分配的结构数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:05 25 4
gpt4 key购买 nike

我正在使用 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() 的调用并将其替换为如下所示的硬编码数量后,您的代码可以正常工作:

#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

关于c - 使用在 C 中动态分配的结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10501575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com