【问题标题】:Use an array of struct dynamically allocated in C使用在 C 中动态分配的结构数组
【发布时间】:2012-05-17 02:40:40
【问题描述】:

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

由于某种原因,当我的数组由

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

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

感谢您的帮助!

【问题讨论】:

  • 不应该resultentity **
  • 不,这对我来说基本上可以正常工作(一旦我添加了缺少的标题,声明计数变量并用文字 4 替换省略的 random_int 函数)。你可以试试同样的方法(硬编码int count = 4),看看它是否仍然失败?如果是这样,请发布仍然显示问题的最小可编译代码
  • @user 你在做什么没有错(ideone.com/V4jgG)。显示 random_int 函数在做什么。并且可能你没有展示你到底在做什么。
  • 你是怎么编译的?编译器抱怨什么? count 声明在哪里?
  • 您是否检查过callocstrdup 没有失败并返回NULL?也许你的模拟器不知何故内存不足?

标签: c arrays dynamic struct allocation


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 2012-01-22
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多