开发者

Scope of malloc used in a function

When a function returns, is the memory allocated via malloc freed? Or can it still be accessed in the main() function using pointers?

eg.

void function(int *a)
{
    a=(int *)malloc(sizeof(int));
    *a=10;
}
int main()
{
   开发者_StackOverflow中文版 int *num;
    function(num);
    printf("%d",*num);
    return(0);
}

Can the integer stored in a be accessed by main() here?


No, the memory allocated with malloc is not freed when you leave the scope/return from the function.

You're responsible for freeing the memory you malloc.

In your case though, the memory is NOT accesible in main(), but that's because you only deal with a local variable.

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

Here, a is a local variable within function . Pointers are passed by value in C, so a receives a copy of the pointer in main when you do function(num); main() does not see that you assign to that local copy of the pointer.

You have to do either:

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

or

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 memory is only freed when you call free() on it. It can be accessed by anybody with a valid pointer to it until that time.


No. You are passing the pointer numby value, hence the changes made by the function will not be reflected in main. So effectively there is no way to access/free the allocated memory from main

To fix this you can pass the address of num or return a from function and collect the returned value in num


Memory is not freed. Any function can allocate memory and any other can deallocate it. It's a real mess if you're not super-finicky, until... someone invented the Garbage Collection.


malloc is working fine (though you will have to call free() on the pointer it returns). The problem here is that you aren't returning a pointer to the memory it allocated.

"int * a", your parameter to function() is the address of an integer. The usual way to return that would be to rewrite your function as follows:

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

To return it via a parameter, you need to return the address of the pointer:

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

And now you know why they invented C#.

Here's a way to rewrite your allocation thing so it's more clear:

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


You can store the direct address of the allocated memory in a list container then create a function to loop, access each address into a free function, and then pop out the address. You can insert the address directly into the free function like free(myaddresslist.front()); myaddresslist.pop_front(); . This is a quasi way of doing your own garbage collection without having to change your entire project to GC based languages. Use myaddresslist.size() to make sure you don't call free() on an empty field (resulting in a crash) and to determine the number of loops to take.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜