In C you only “free” memory that was obtained from the heap (i.e., from malloc / calloc / realloc / strdup).
Everything else—string literals, arrays declared on the stack, global/static variables—must not be passed to free().
──────────────────────────────────────────
Heap-allocated character buffer
──────────────────────────────────────────
c
char *buf = malloc(100); // or calloc / realloc / strdup / asprintf ...
if (!buf) { /* handle error */ }
/* … use buf … */
free(buf); // releases the block
buf = NULL; // (optional) avoids an accidental use-after-free
Key points
• Call free exactly once on the address you got back from malloc (or its friends).
• After free the pointer becomes invalid; do not read, write, or free it again.
• Setting the pointer to NULL after freeing is a defensive habit that makes a subsequent free(buf) a harmless no-op.
──────────────────────────────────────────
2. Don’t free what was never malloc’ed
──────────────────────────────────────────
──────────────────────────────────────────
3. Freeing nested pointers
──────────────────────────────────────────
If you have arrays of pointers (e.g., char **argv), free the inner blocks first, then the outer:
c
char **lines = malloc(n * sizeof *lines);
for (size_t i = 0; i < n; ++i)
{
lines[i] = strdup(some_text); // allocate each line
}
/* … use lines … */
for (size_t i = 0; i < n; ++i)
free(lines[i]); // free each inner char*
free(lines); // finally free the whole array
──────────────────────────────────────────
4. Common pitfalls
──────────────────────────────────────────
• Mixing malloc/free with new/delete (C++) – never do that.
• Freeing the pointer returned by tokenizing functions such as strtok() (don’t; they return inside existing strings).
• Freeing a substring pointer that is not the original malloc address.
c
char *full = malloc(50);
char *sub = full + 10; // points inside the block
free(sub); // ❌ free **full**, not sub
──────────────────────────────────────────
5. Quick checklist
──────────────────────────────────────────
✅ Was the memory obtained with malloc / calloc / realloc / strdup?
✅ Have you freed it exactly once?
✅ Have you stopped using the pointer afterwards (or set it to NULL)?
If you can answer “yes” to all three, you’re freeing your char array pointer correctly.
User
If I char *buf = malloc(100);
Can the char[] be 100 characters long?
Assistant
malloc(100) gives you 100 bytes of raw storage.
Because char is defined to occupy exactly one byte, you can put 100 char values in that block.
However, in C the term “string” normally means a null-terminated sequence of characters.
If you plan to treat the buffer as a char string, you must reserve one extra byte for the terminating '\0'.
So:
• As a plain character buffer (no '\0' needed):
– You can store 100 characters, indexed buf[0] … buf[99].
• As a C string:
– You can store at most 99 visible characters and then place buf[99] = '\0';.
– To hold a 100-character string you’d allocate malloc(101) instead.
Tiny rule of thumb:
c
size_t n = 100; // desired maximum string length
char *buf = malloc(n + 1); // +1 for the '\0'