【问题标题】:Allocating structure memory dynamically t times in loop instead of declaring array of structure在循环中动态分配结构内存 t 次,而不是声明结构数组
【发布时间】:2017-05-31 16:34:12
【问题描述】:

Original code (using array of structure): 在这段代码中,我将t 作为用户的输入,并声明一个大小为t 的结构数组tc,然后进行一些处理。

#include<stdio.h>
int main()
{
    int t,i,j,k,min=0;
    //# of test cases
    scanf("%d",&t);

    struct testcase
    {
        int sizeOfArray;
        int a[10];
        int b[10];
        int ans;
    };

    struct testcase tc[t];            //declaring array of structures, size t 

    for(i=0;i<t;i++)
        {
            scanf("%d",&tc[i].sizeOfArray);  //entering size of a and b
            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of a
                    scanf("%d",&(tc[i].a[j]));

            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of b
                    scanf("%d",&tc[i].b[j]);                    
        }
    int no=0;
    for(k=0;k<t;k++)
        {
            min=    tc[k].a[0]+tc[k].b[1];
            for(i=0;i<tc[k].sizeOfArray;i++)
                {
                    for(j=0;(j<tc[k].sizeOfArray);j++)
                        {
                            if((tc[k].a[i]+tc[k].b[j]<min)&&(j!=i))                        
                                    min=tc[k].a[i]+tc[k].b[j];
                        }
                }
            tc[k].ans=min;
            printf("%d\n",min);
        }
    return 0;
}

What I have tried: 这里不是声明大小为 t 的结构数组,而是在 for 循环中动态分配结构内存并进行相同的处理。

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int t,i,j,k,min=0;
    //# of test cases
    scanf("%d",&t);
    struct testcase
    {
        int sizeOfArray;
        int a[10];
        int b[10];
        int ans;
  };

    struct testcase *tc = NULL;          

    for(i=0;i<t;i++)
        {
            struct testcase* tc = malloc(20 * sizeof(*tc));
            scanf("%d",&tc[i].sizeOfArray);  //entering size of a and b
            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of a
                    scanf("%d",&(tc[i].a[j]));

            for(j=0;j<tc[i].sizeOfArray;j++)   //entering elements of b
                    scanf("%d",&tc[i].b[j]);                   
        }
    int no=0;
    for(k=0;k<t;k++)
        {
            min=tc[k].a[0]+tc[k].b[1];
            for(i=0;i<tc[k].sizeOfArray;i++)
                {
                    for(j=0;(j<tc[k].sizeOfArray);j++)
                        {
                            if((tc[k].a[i]+tc[k].b[j]<min)&&(j!=i))
                                    min=tc[k].a[i]+tc[k].b[j];
                        }
                }
            tc[k].ans=min;
            printf("%d\n",min);
        }
    return 0;
}

Question:为什么第二个代码不起作用?在第二个代码中需要进行哪些更正,我是否正确使用了malloc?,malloc 是否正确放置?还是有任何语法错误或逻辑错误?

【问题讨论】:

  • 每次循环都会分配一个新数组,tc 变量是循环体的局部变量,因此在下一个循环中无法访问。在循环之前分配一次。
  • 为什么您认为首先需要动态分配它?原版有什么问题?
  • 在调用任何堆内存分配函数(malloc、calloc、realloc)时,始终检查(!=NULL)返回值以确保操作成功
  • @Barmer ,这里的 malloc 应该只存在一次并且也在循环之外,然后它会像以前一样工作(如 struct array tc[20]),对吧?

标签: c arrays pointers malloc structure


【解决方案1】:

你需要动态分配一个struct testcase的数组,所以你只需要做一次,在你进入循环之前。

struct testcase *tc = malloc( t * sizeof(struct testcase));
if (!tc) {
    perror("malloc failed");
    exit(1);
}

for(i=0;i<t;i++)
...

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多