Dynamically allocate and free memory in local functions

Consider the following function:

void free_or_not ( int count ) {
    int i ;
    int *ip = malloc ( count * sizeof ( int ) ) ;

    for ( i = 0 ; i < count ; i ++ )
        ip[i] = i ;

    for ( i = 0 ; i < count ; i ++ )
        printf ( "%d\n" , ip[i] ) ;

    free ( ip ) ;
}

Will it cause a memory leak if I don't call free()inside free_or_not()?

+4
source share
2 answers

Yes, when your function ends, you will lose the pointer to the allocated memory to free ().

+3
source

Will it cause a memory leak if I don't call free()inside free_or_not()?

Yes, it will cause a memory leak. Once your function completes, you have no pointers to the allocated memory on free().

:. void * ( ) (ip) , tn free() .

+7

All Articles