【问题标题】:Struct Array Dynamic Memory Allocation in CC中的结构数组动态内存分配
【发布时间】:2021-10-15 18:50:58
【问题描述】:

我正在尝试使用动态内存分配创建一个结构数组。我的结构是这样的:

typedef struct {
    char data1[200];
    char data2[200];
    char data3[200];
    int data4;
    double data5;
    double data6;
} randomInfo_t;

我不知道我需要创建多少个这些结构,这就是我使用malloc() 动态创建数组的原因。

一个文件中存储了多行数据,类似于 CSV。

现在,我创建数组的尝试导致了分段错误,我怀疑这是由于我使用 malloc() 的方式造成的:

randomInfo_t *stuffArray;
int indexCounter=0;

while(fgets(buffer, 1024, fp)) {

    /* some random code here */

    stuffArray[indexCounter] = *(randomInfo_t *)malloc(sizeof(randomInfo_t *));
    assert(stuffArray[indexCounter] != NULL);

    char *readLine = strtok(buffer, " ");

    while(readLine) {
            strcpy(randomArray[indexCounter].data1, readLine);
            
                   etc...
    }

    indexCounter++;

}

我自己尝试过调试,但完全不知道自己在做什么。

感谢任何帮助。 谢谢

(PS。我知道使用链表会容易得多,我也更喜欢使用一个,但代码要求意味着没有链表)。

【问题讨论】:

  • stuffArray 是一个指针。它指向什么?
  • 您想为randomInfo_t 的数组分配内存,还是为randomInfo_t * 的数组分配内存,每个元素都指向一个单独分配的randomInfo_t?你的代码全错了,所以很难判断你要实现哪个选项。
  • 在这两种情况下,您都可以使用realloc动态扩展stuffArray内存。
  • @IanAbbott 我注意到您有使用 C 的经验。请您看看我的回答并告诉我是否有任何明显的错误。由于问题的性质,很难对其进行测试。

标签: c struct segmentation-fault malloc dynamic-memory-allocation


【解决方案1】:

不清楚您是尝试动态分配randomInfo_t 数组还是randomInfo_t * 数组,每个元素都指向randomInfo_t

假设您正在动态分配randomInfo_t 的数组,您可以使用realloc 逐渐增加数组的大小:

randomInfo_t *stuffArray=NULL;
int indexCounter=0;

while(fgets(buffer, 1024, fp)) {

    /* some random code here */

    randomInfo_t *oldStuffArray = stuffArray; /* In case realloc fails */
    stuffArray = realloc(stuffArray, sizeof(*stuffArray) * (indexCounter + 1));
    if (stuffArray == NULL) {
        /*
         * realloc failed. Take appropriate action here.
         * Note that the memory pointed to by oldStuffArray is still valid.
         */
#if FOO
        /* for example: */
        fprintf(stderr, "Memory allocation failed\n");
        free(oldStuffArray);
        exit(1);
#else
        /* or you could use oldStuffArray contents and not read any more: */
        fprintf(stderr, "Memory allocation failed\n");
        stuffArray = oldStuffArray;
        break;
#endif
    }

    /* Do stuff with stuffArray[indexCounter] */

    indexCounter++;
}

【讨论】:

    【解决方案2】:

    问题是stuffArray 是一个未初始化的指针。您需要做的是初始化它并跟踪其当前大小,如下所示:

    // allocate space for 10 pointers to struct
    randomInfo_t **stuffArray = malloc(sizeof(randomInfo_t*) * 10); 
    int stuffArray_size = 10;
    int stuffArray_used = 0;
    

    然后,当您为另一个结构指针分配空间时,您可以这样做:

    // if there are more structs than what stuffArray can currently fit realloc
    if(indexCounter >= stuffArray_size) {
        stuffArray_size += 10;
        stuffArray = realloc(stuffArray, stuffArray_size * sizeof(randomInfo_t*));
    }
    stuffArray_used++;
    // now we can allocate space for a new struct and point stuffArray[indexCounter] to the newly allocated space
    stuffArray[indexCounter] = malloc(sizeof(randomInfo_t));
    

    现在当你想访问结构成员时,你必须使用->。 所以这一行:

    strcpy(randomArray[indexCounter].data1, readLine);
    

    现在必须是:

    strcpy(randomArray[indexCounter]->data1, readLine);
    

    此外,当您使用完数组后,您必须free 它。 像这样:

    // this is where the stuffArray_used comes into play, we don't want to try freeing unallocated memory
    for(int i = 0; i < stuffArray_used; i++){
        free(stuffArray[i]);
    }
    free(stuffArray);
    

    【讨论】:

    • 请注意,我没有测试就写了这个,因为用你提供的代码测试它有点棘手,但它应该给你一个大致的想法。
    • 既然你想要评论,除了if(indexCounter &gt;= size) { 我认为你的意思是if(indexCounter &gt;= stuffArray_size) { 之外,它看起来还不错。并且需要将realloc返回的指针存储回stuffArray
    • @IanAbbott 感谢您指出这一点。现在修复它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2013-12-25
    • 2020-04-18
    相关资源
    最近更新 更多