memory

char *copy = strdup("payload");
if (copy == NULL) {
    return;
}
free(copy);

That ran cleanly

malloc

malloc returns raw memory or NULL The memory is uninitialized Use calloc when zeroed bytes are required

Free once

Every successful allocation needs exactly one free Freeing twice corrupts allocator state Using memory after free is worse because it can look fine for a while

Ownership transfer

If a function returns allocated memory, document it If a function frees a pointer, document it louder Most C memory bugs are contract bugs before they are code bugs

C23 change

Calling realloc with a zero size is no longer permitted Treat that as undefined behavior Handle the zero case before calling it

Next

Files in 10 files

Posted