tags:

views:

102

answers:

5

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()
{
    int *num;
    function(num);
    printf("%d",*num);
    return(0);
}

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

+2  A: 

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.

Steve Townsend
A: 

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.

vulkanino
+3  A: 

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

codaddict
+8  A: 

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);
  return(0);
}

or

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

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;
}
Ed J. Plunkett