函数中使用的malloc的范围

当函数返回时,通过malloc分配的内存是否已释放? 或者仍然可以使用指针在main()函数中访问它?

例如。

void function(int *a) { a=(int *)malloc(sizeof(int)); *a=10; } int main() { int *num; function(num); printf("%d",*num); return(0); } 

存储在a中的整数可以通过main()访问吗?

不,当您从函数中退出范围/返回时,不会释放分配有malloc的内存。

你有责任释放你的malloc内存。

但是在你的情况下,内存在main()中是不可访问的,但那是因为你只处理一个局部变量。

 void function(int *a) { a=(int *)malloc(sizeof(int)); 

这里, afunction内的局部变量。 指针在C中传递值,因此当你执行function(num);时,它会在main中接收指针的副本function(num); main()没有看到您分配给指针的本地副本。

你必须这样做:

 void function(int **a) { *a= malloc(sizeof(int)); **a=10; } int main() { int *num; function(&num); printf("%d",*num); free(num); return(0); } 

要么

 int* function(void) { int *a= malloc(sizeof(int)); *a=10; return a; } int main() { int *num; num = function(); printf("%d",*num); free(num); return(0); } 

malloc() ed内存仅在您调用free()时释放。 任何拥有有效指针的人都可以访问它,直到那个时间。

不是。您通过值传递指针num ,因此function所做的更改不会反映在main 。 所以实际上没有办法从main访问/释放分配的内存

要解决此问题,您可以传递num的地址或从函数返回a并在num收集返回的值

malloc工作正常(虽然你必须在它返回的指针上调用free())。 这里的问题是你没有返回指向它分配的内存的指针。

“int * a”,函数()的参数是整数的地址。 返回的常用方法是重写函数,如下所示:

 int * function() { int * a = (int *)malloc(sizeof(int)); *a = 10; return a; } 

要通过参数返回它,您需要返回指针的地址:

 // pp points to a pointer void function( int ** pp ) { // Assign allocated memory to the thing that pp points to *pp = (int *)malloc( sizeof( int ) ); // Get the thing pp points to -- a pointer. // Then get the thing which THAT pointer points to -- an integer // Assign 10 to that integer. **pp = 10; } void main() { int * p = NULL; function( & p ); printf( "%d\n", *p ); free( p ); } 

现在你知道他们为什么发明了C#。

这是一种重写分配事物的方法,因此更清楚:

 void function( int ** pp ) { int * newmemory = (int *)malloc( sizeof( int ) ); // Assign 10 to the integer-sized piece of memory we just allocated. *newmemory = 10; // Assign allocated memory to the thing that pp points to. *pp = newmemory; } 

内存未被释放。 任何函数都可以分配内存,任何其他函数都可以释放它。 如果你不是超级挑剔,那真是一团糟,直到…有人发明了垃圾收集。

您可以将已分配内存的直接地址存储在列表容器中,然后创建一个循环函数,将每个地址访问为一个自由函数,然后弹出该地址。 您可以将地址直接插入免费function,如free(myaddresslist.front()); myaddresslist.pop_front(); free(myaddresslist.front()); myaddresslist.pop_front(); 。 这是进行自己的垃圾收集的一种准方式,无需将整个项目更改为基于GC的语言。 使用myaddresslist.size()确保不在空字段上调用free()(导致崩溃)并确定要采用的循环数。