我是否正确使用malloc?

我是否正确使用malloc?,c,malloc,C,Malloc,你好 我需要使用malloc创建一个学生列表系统。。。。为了提高效率,我们的教授要求我们在结构上使用它,所以我创建了一个结构,如下所示: struct student { char studentID[6]; char name[31]; char course [6]; }; struct student *array[30]; 每次我添加一条记录,也就是当我使用malloc时 array[recordCtr]=(struct student*)ma

你好

我需要使用malloc创建一个学生列表系统。。。。为了提高效率,我们的教授要求我们在结构上使用它,所以我创建了一个结构,如下所示:

struct student {
       char studentID[6];
       char name[31];
       char course [6];
};
struct student *array[30];
每次我添加一条记录,也就是当我使用malloc时

array[recordCtr]=(struct student*)malloc(sizeof(struct student));
recordCtr++;
然后我就这样把它放了

 for(i = 0; i < recordCtr; i++){
       free(array[i]);
  } 
提前谢谢。非常感谢您的意见。

您做得很好


free(数组)将是未定义的行为,因为
数组
本身未通过
malloc
分配,因此您不能
释放它,也不需要-内存将由编译器管理。

数组本身未在堆上分配。假设它是一个全局变量,它在程序启动时被分配到全局内存中,不需要释放。使用免费电话可能会损坏您的程序


您当前的解决方案是正确的。

您所做的是正确的

您可以将
*array[30]
看作是一个包含30个指针的数组
为这些指针分配内存时,还需要对每个指针调用free()。

是的,您使用的是正确的。有比这更好的方法来组织存储,但这会起作用。至少在你需要30多名学生之前

请注意,必须使用
malloc()
返回的每个指针调用
free()
。这意味着您在指针数组上的循环是所选体系结构的正确方法

您尝试在阵列本身上调用free将不起作用。它调用未定义的行为,因为您正在向
free()
传递一个指针(指向数组本身的基部),该指针不是来自对
malloc()

的调用

您可以(如果它适合您的问题)一次性为所有30个结构分配空间

 struct student *array = (struct student *)malloc(30*sizeof(struct student));
当你想处理空间时,你可以这样做

free(array)

你所拥有的会很好用的。正如其他人所提到的,您已经在堆栈上创建了一个指针数组,并且需要在执行时逐个malloc和释放它们

但是,您不必一次使用malloc并释放一个struct,您可以这样做:

int arraySize = 30;
student * ourArray = (student*)malloc(sizeof(student) * arraySize);
指针上的一个自由键将处理它。有了这个指针,您仍然可以使用括号表示法,编译器将理解它是一个类型化指针,并且可以适当地进行操作,从而提供基本相同的内容。使用哪种方法取决于您是否需要数组的动态大小以及个人偏好


希望对您有所帮助。

使用空值初始化指向struct student的指针数组

for(i = 0; i < recordCtr; i++){
       array[i] = NULL;
  } 
for(i=0;i
如果数组[i]不为空,则释放内存

for(i = 0; i < recordCtr; i++){
       if(NULL != array[i])
       {
           free(array[i]);
       }
  } 
for(i=0;i
一个好的建议是始终做到:

type *something;
something = malloc(n * sizeof(*something));
这是因为,如果您更改了某个代码的类型,则不必更改所有其他代码。sizeof实际上是一个编译器操作,它在运行时不会发生任何变化

另外,不要强制转换malloc返回的void*指针,在C中没有这样做的理由,它只是进一步将代码绑定在一起

因此,在您的情况下,不要:

(struct student*)malloc(sizeof(struct student));
但是


您使用malloc的方式并不违法,但这不是一个列表,而是一个指针数组

要使用列表,您不需要预先确定大小,而是要有一个指向下一个元素的指针。您可以将此设置为介入式或非介入式

对于侵入性列表,您将
struct student*next
放在student声明中。 对于非侵入式列表,创建另一个struct student_list_节点,该节点包含struct student实例和指针struct student_list_node*next

这是非侵入式版本的一个示例:

struct student_list_node
{
  struct student data;
  struct student_list_node * next;
};

struct student_list_node * head;
struct student_list_node * tail;

struct student_list_node * addStudentToTail()
{
   struct student_list_node * newnode = (struct student_list_node *)(malloc( sizeof(struct student_list_node ) );
   /* check malloc did not fail or use a checking vesrion of malloc */
   if( !tail )
   {
      head = tail = newnode;
   }
   else
   {
      tail->next = newnode;
      tail = newnode;
   }
   return newnode; // which is also "tail"
}

int main()
{
   struct student_list_node * node = addStudentToTail();
   struct student * pstud = &node->data;
   /* write to pstud student details */
}
如果您确实想使用数组,您可能希望将其设置为student而不是student*数组,在这种情况下,您可以使用calloc而不是malloc

struct student*数组=(struct student*)calloc(30,sizeof(student))


然后使用
free(array)
将是正确的处理方法。如果以后需要,您还可以使用realloc分配更多。(注意这一点:在知道realloc成功之前,必须保留原始指针的副本)

有一个简单的规则:每个
malloc()
都应该与
free()
和malloc返回的指针配对。不要少,不要多。

waah。对我来说太复杂了。无论如何,谢谢。我将尝试研究这个。我可以知道为什么我需要初始化数组并将其设置为NULL吗?谢谢。@newbie:除非初始化指针数组,否则它们可能包含垃圾值。如果您试图释放一个垃圾值,那么您的C运行时就会在一堆冒烟的熔渣中崩溃。可以执行
free(NULL)
。在释放循环时,不需要执行空指针检查。
malloc(sizeof(**array));
struct student_list_node
{
  struct student data;
  struct student_list_node * next;
};

struct student_list_node * head;
struct student_list_node * tail;

struct student_list_node * addStudentToTail()
{
   struct student_list_node * newnode = (struct student_list_node *)(malloc( sizeof(struct student_list_node ) );
   /* check malloc did not fail or use a checking vesrion of malloc */
   if( !tail )
   {
      head = tail = newnode;
   }
   else
   {
      tail->next = newnode;
      tail = newnode;
   }
   return newnode; // which is also "tail"
}

int main()
{
   struct student_list_node * node = addStudentToTail();
   struct student * pstud = &node->data;
   /* write to pstud student details */
}