Where the storage comes from
Storage that outlives a function call
A function can calculate a number and return it. Returning access to an array takes more care: the caller needs storage that is still alive when it uses the elements. The type names from the previous section describe what an object holds; its storage duration tells us how long that object exists.
Three lifetimes you will use
| Storage duration | Familiar example | When its lifetime ends |
|---|---|---|
| Automatic | An ordinary local int or fixed-size array | When execution leaves its block, including by returning |
| Static | A global variable, a local declared static, or a string literal | At the end of the program |
| Allocated | Storage obtained with malloc or calloc | When you release it with free |
These are the durations needed here, not an exhaustive list of C's storage categories. C also has thread storage duration, which this course does not use. A local static variable keeps its value between calls, even though its name is only usable inside its scope. A string literal such as "Ready" has static duration; that does not make its characters writable.
Reading a stack-and-heap picture
When main calls a helper, main waits while the helper runs. We often draw the active calls as stack frames: one frame for the caller and another for the callee. Ordinary local variables belong to the current call. Returning removes the callee's frame from this model.
Allocated storage is usually called the heap. We draw it outside those frames because returning from a function does not release it. Stack and heap are a conventional implementation model, not a memory layout required by C. There are no promised numeric addresses, growth directions, or gaps between these boxes. An optimizing compiler may keep locals in registers or inline a call; the lifetime rules still apply.
Read the rows as successive moments for one allocation: the left box is a pointer object, and the right box is the separately allocated integer.
A pointer is not the object it points to
In the picture, the helper's pointer p is an automatic object. The integer obtained from malloc is a different object, with allocated duration. p holds its address. Returning that address copies a pointer value to the caller; it does not move the allocated integer or extend the helper's local variables.
Follow this complete example now, then run the smaller allocation demo in the next lesson. <stdlib.h> declares malloc and free. NULL means that no usable object was obtained. The next lesson explains the byte count in the request.
#include <stdio.h>
#include <stdlib.h>
/* On success, the caller owns the returned allocation. */
int *make_count(void) {
int *p = malloc(sizeof *p);
if (p == NULL) {
return NULL;
}
*p = 6;
return p;
}
int main(void) {
int *count = make_count();
if (count == NULL) {
return 1;
}
printf("Count: %d\n", *count);
free(count);
count = NULL;
return 0;
}
The successful path prints Count: 6. While the helper runs, p points to the new integer. After return p, the local variable p is gone, but the integer remains alive and count points to it. We call main the owner because it now has the responsibility to release that allocation. Ownership is an agreement in our program, not a check that C performs for us.
Contrast that with declaring int local = 6; inside the helper and returning &local. That would return the address of an automatic object whose lifetime ends as the helper returns. The caller must not use that address. Returning an ordinary value such as local is fine: the integer value is copied before the local object's lifetime ends.
After free(count), the allocated integer is gone too. Do not read it, write it, or free it again through any old pointer. Assigning NULL to count records that this variable no longer points to usable storage; it does not repair other copies of the old address. free(NULL) does nothing, but dereferencing NULL is invalid.
If the helper lost its only pointer without returning it or freeing it, the allocation would still exist with no way for the program to release it. That is a memory leak. Keep one clear owner and release each successful allocation exactly once, after its final use. Never pass the address of an automatic variable or a string literal to free.