简体   繁体   English

当我使用**(双指针)时如何使用免费?

[英]How can I use free when I used ** ( double pointer )?

STUDENT ** list=NULL;
char *getid;
getid =(char*)malloc(sizeof(char) * 8);
printf("How many student? ");
int menuNum,num=0;

scanf("%d",&num);
list=(STUDENT**)malloc(num*sizeof(STUDENT*));

I used pointer like this. 我用这样的指针。 As I learned from the professor, before finishing my code, I ought to use free() function to retrieve the allocated memory again. 正如我从教授那里学到的,在完成我的代码之前,我应该使用free()函数再次检索分配的内存。

Here s what i wanna ask you. 这就是我想问你的问题。

I learned that If i wanna use free() about (char *getid) 我了解到如果我想使用free()about(char * getid)

I know I should write 我知道我应该写

free(getid);

then How can I use free about 那么我怎样才能免费使用

STUDENT ** list = NULL ; // **It's about struct**

Should I use like 我应该喜欢用吗?

free(list);

or 要么

free(*list);

I think the former is right, but when I write like the latter, there's no error on my X-code. 我认为前者是对的,但是当我像后者一样写时,我的X代码没有错误。

Could you tell me about it ? 你能告诉我一下吗?

You should use: 你应该使用:

free(list);

but when I write like free(*list);, there's no error on my X-code 但是当我写得像free(* list);时,我的X代码没有错误

That's why *list does not deallocate anything, since nothing is allocated there. 这就是为什么*list不会解除分配任何东西,因为那里没有分配任何内容。 However, your program suffers from a memory leak that way, since the memory pointed to by the double pointer named list is not free'd. 但是,您的程序会遭遇内存泄漏,因为名为list的双指针指向的内存不是免费的。 However, you are just (un)lucky not to see your program crash. 但是,如果没有看到你的程序崩溃,那你很幸运。

If you used Valgrind though, you would see the memory leak. 如果你使用Valgrind ,你会看到内存泄漏。

Should I write malloc and write free() immediately? 我应该立即编写malloc并写free()吗? not in the end of the line? 不是在最后?

No. You first allocate the memory dynamically with malloc() , then you use this memory (initialize, access, etc.) and when you do not need the memory anymore, and only then, you deallocate it with free() . 不。您首先使用malloc()动态分配内存,然后使用此内存(初始化,访问等),当您不再需要内存时,只有这样,您才能使用free()解除分配。


However, if you had dynamically allocated space for list[i] , then you should do: 但是,如果你为list[i]动态分配了空间,那么你应该这样做:

free(list[i]);
free(list);

where the order matters, since you do not want to have tangling pointers! 订单很重要,因为你不想有纠结的指针!

Maybe my example on dynamically allocated a 2D array will more explanatory on this, even though it's a different data structure than a list. 也许我动态分配2D数组的例子会对此有更多解释,即使它是一个与列表不同的数据结构。


PS: We don't cast what malloc returns . PS:我们不会抛出malloc返回的内容

You should free any memory allocated by malloc with free... 你应该免费释放malloc分配的任何内存...

as you figured out, you requested 8 bytes of memory and saved the Pointer to it to your symbol getid ( getid =(char*)malloc(sizeof(char) * 8); ) 如你getid ,你要求8个字节的内存并将指针保存到你的符号getidgetid =(char*)malloc(sizeof(char) * 8);

as for your list, this is a bit trickier: 至于你的清单,这有点棘手:
You actually allocating a list of pointers that should point to other memory locations (those might be dynamically allocated as well) 您实际上是在分配一个指向其他内存位置的指针列表(也可能是动态分配的)

list=(STUDENT**)malloc(num*sizeof(STUDENT*)); size of num pointers num指针的大小
Allocates the space for the list and saves the pointer to it at symbol list . 为列表分配空间并在符号list保存指向它的指针。

I'd remind that the memory allocated doesn't have to come initialized from the OS, so we should initialize it. 我提醒分配的内存不必从操作系统初始化,所以我们应该初始化它。

for(int i=0; i<num; i++)
{
   list[i] = NULL;
}       

You could also use memset(list, NULL, num * sizeof(STUDENT*)) . 您还可以使用memset(list, NULL, num * sizeof(STUDENT*))

and you are very much correct you should free its memory by free(list) , 并且你非常正确你应该通过free(list)释放它的记忆,
But you should free the items in the list BEFORE you free the list of pointers itself. 但是,在释放指针列表之前,您应该释放列表中的项目。 (again, if the items were dynamically allocated!) (再次,如果项目是动态分配的!)

for(int i=0; i<num; i++)
{
   if(list[i] != NULL) // only if allocated.
   {
      free(list[i]); // free the element @ position i
      list[i] = NULL;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM