【问题标题】:Scope of malloc used in a function函数中使用的 malloc 范围
【发布时间】:2016-01-30 02:12:20
【问题描述】:

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

例如。

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

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

【问题讨论】:

    标签: c malloc


    【解决方案1】:

    不,当您离开函数的作用域/返回时,不会释放使用 malloc 分配的内存。

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

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

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

    这里,afunction 中的一个局部变量。指针在 C 中按值传递,因此 a 在您执行时会在 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);
    }
    

    【讨论】:

      【解决方案2】:

      malloc()ed 内存只有在你调用free() 时才会被释放。在此之前,任何拥有指向它的有效指针的人都可以访问它。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

          “int * a”,你给function()的参数是一个整数的地址。返回的通常方法是重写您的函数,如下所示:

          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;
          }
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              【解决方案6】:

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

              【讨论】:

                猜你喜欢
                • 2011-12-27
                • 2013-05-16
                • 2015-08-01
                • 1970-01-01
                • 2016-07-26
                • 1970-01-01
                • 2021-10-04
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多