I understand that variables declared inside a function have automatic storage . Does this mean that there is no need for pointers free( pbelow) that are explicitly highlighted in the function call with malloc?
void f() {
int *p;
p = malloc(sizeof(int));
*p = 1;
printf("%p %d\n", p, *p);
free(p);
}
int main() {
f();
}
(Note that I used a pointer to here int, but the question applies to any pointer returning a value mallocand friends.)
I guess not, because it mallocallocates memory on the heap, not the stack; thus, the memory point pindicates that it will not be freed automatically when the stack frame for is inserted f().
source
share