I understand that variables declared inside a function have automatic storage . Does this mean that there is no need for pointers free
( p
below) 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 malloc
and friends.)
I guess not, because it malloc
allocates memory on the heap, not the stack; thus, the memory point p
indicates that it will not be freed automatically when the stack frame for is inserted f()
.
source
share