C-malloc用于指向结构的指针 [英] C - malloc for a pointer to struct

查看:91
本文介绍了C-malloc用于指向结构的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码可以工作,但是我还没有弄清楚malloc如何将内存分配给C中的struct指针.在我的代码中,我没有将struct的整个大小分配给我的指针,而只是分配了大小成员的好坏?而且如果不好,我应该怎么做才能优化我的代码.

My code works, but i haven't figured out how memory is allocated by malloc to pointer to struct in C. And in my code, i didn't allocate the whole size of my struct to my pointer but only the size of its member, is it good or bad? And if it's bad, what should i do to optimize my code.

这是我的代码:

struct Student
{
    char name[25];
    float math, physic;
    char rank;
};



void studentManagement(void)
{
struct  Student *s;
int n,i=1;
float ave;

printf("Number of student = ");
scanf("%d", &n);
getchar();

    while(i<=n)
    {
         s = malloc(25*sizeof(char));

         printf("Enter student name: ");
         fgets(s->name,25,stdin);             
         free(s);

         s = malloc(5*sizeof(float));

         printf("Physics grade = ");
         scanf("%f", &s->physic);
         printf("Math grade = ");
         scanf("%f", &s->math);

         getchar();

         ave = ((s->math )+ (s->physic)) / 2;

         if(ave<5) 
             printf("Rank = D\n");
         if(ave >= 5 && ave <= 7) 
             printf("Rank = C\n");
         if(ave >= 7 && ave <= 8) 
             printf("Rank = B\n");
         if(ave > 8) 
             printf("Rank = A\n");

         free(s);
         i++;
  }
}

推荐答案

s = malloc(25*sizeof(char));中,您正在分配空间并设置s指向该空间. s是指向struct Student的指针.因此,s应该为struct Student指向足够的空间.因此,正确的分配是:

In s = malloc(25*sizeof(char));, you are allocating space and setting s to point to this space. s is a pointer to struct Student. So s ought to point to enough space for a struct Student. So the proper allocation is:

s = malloc(sizeof(struct Student));

另一种方法是使用s指向的东西的大小:

Another way to do this is to use the size of the thing that s points to:

s = malloc(sizeof *s);

通常最好选择后者,因为如果以后更改代码以使s指向其他类型的对象,则后者仍然正确.

The latter is generally preferred because it remains correct if the code is later changed so that s points to a different type of object.

获得名称后,您就拥有free(s);.这是不正确的.仅应在使用完内存后释放内存.但是您仍然想使用s指向的对象,因此释放s为时过早.

Just after getting the name, you have free(s);. This is incorrect. You should only release memory when you are done using it. But you still want to use the object that s points to, so it is too early to free s.

同样,您不需要使用s = malloc(5*sizeof(float));.语句s = malloc(sizeof *s);(如果成功)将为struct Student全部分配足够的内存.您分配一个完整的结构,使用它,然后释放它.您不分配结构的各个部分.

Similarly, you do not need to use s = malloc(5*sizeof(float));. The statement s = malloc(sizeof *s); will (if it succeeds) allocate enough memory for all of a struct Student. You allocate an entire structure, use it, and then free it. You do not allocate pieces of a structure.

一旦那段代码正常工作,您应该考虑为什么要为struct Student分配空间然后很快释放它.如果只想使用struct Student进行一次循环迭代,则可以通过声明struct Student s;而不是分配内存并指向它来实现.您正在进行的分配可能需要构建一个struct Student数组,在这种情况下,您将不得不考虑如何为整个数组分配内存以及该数组中需要多少个元素.

Once you have that piece of code working correctly, you should think about why you allocate space for a struct Student and then free it soon. If you just want to work with a struct Student for one iteration of a loop, you can do it by declaring struct Student s; instead of allocating memory and pointing to it. The assignment you are working on may require building an array of struct Student, in which case you will have to think about how you allocate memory for an entire array and how many elements you will need in the array.

这篇关于C-malloc用于指向结构的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆