Common Pointer and Allocation Errors

suggest change

Incorrect pointer and allocation handling can cause undefined behavior, data corruption, security vulnerabilities, or crashes. A crash is only one possible symptom; code with undefined behavior may also appear to work.

Not Checking for Allocation Failure

Allocation functions return a null pointer when they cannot satisfy a request. Check the result before dereferencing it:

struct record *record = malloc(sizeof *record);
if (record == NULL) {
    /* Report or propagate the allocation failure. */
    return false;
}

record->value = 0;

An allocation request of zero bytes is a special case: the result may be a null pointer or a non-null pointer that must not be used to access an object.

Using sizeof for Allocation

The sizes of most C types vary across implementations. Derive the allocation size from the pointed-to type:

size_t count = 1000;
int *values;
if (count > SIZE_MAX / sizeof *values) {
    /* Multiplication would overflow. */
    return false;
}

values = malloc(count * sizeof *values);
if (values == NULL) {
    return false;
}

The overflow check matters when count is not a small known constant. Standard C also provides calloc, which still requires callers to handle allocation failure.

Ownership and Lifetime

Every dynamically allocated object needs a clear owner responsible for eventually calling free. Losing the last pointer to a live allocation creates a memory leak. Accessing an allocation after free, freeing it twice, or passing free a pointer not returned by an allocation function has undefined behavior.

Setting an owning pointer to NULL after free can prevent accidental reuse through that pointer, but it does not invalidate or update aliases:

free(values);
values = NULL;

Memory-analysis tools and compiler sanitizers can help find these errors, but they do not replace explicit ownership rules.

Returning the Address of an Automatic Object

Taking an address does not extend an object's lifetime:

int *invalid_pointer(void)
{
    int value = 10;
    return &value; /* Wrong: value's lifetime ends on return. */
}

value has automatic storage duration. Its lifetime ends when execution leaves the block, regardless of whether the implementation uses a machine stack. Dereferencing the returned pointer has undefined behavior.

The caller can supply storage instead:

void set_value(int *destination)
{
    *destination = 10;
}

int main(void)
{
    int value;
    set_value(&value);
    return value == 10 ? 0 : 1;
}

Alternatively, return the value directly or allocate an object dynamically and clearly document that the caller owns it.

Incrementing and Dereferencing

Postfix ++ and -- bind more tightly than unary *:

*pointer++;   /* Equivalent to *(pointer++): advance the pointer. */
(*pointer)++; /* Increment the pointed-to value. */

The first expression evaluates the object previously pointed to and advances pointer; it does not increment that object. The analogous distinction applies to *pointer-- and (*pointer)--.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents