To properly free a char array pointer in C, you must ensure that the pointer was dynamically allocated using functions like malloc, calloc, or realloc. Here's a concise and correct approach:
✅ Steps to Free a Char Array Pointer
Ensure the pointer was dynamically allocated:
Use malloc, calloc, or realloc to allocate memory.
Do not attempt to free() a pointer to a static array or a string literal (e.g., char *str = "hello";), as this leads to undefined behavior.
Call free():
Use free(ptr) to release the memory allocated to the pointer.
Set the pointer to NULL:
After freeing, assign ptr = NULL to avoid dangling pointers (pointers to memory that has already been freed).
📌 Example Code
c
#include <stdio.h>
#include <stdlib.h>
int main() {
// Allocate a char array dynamically
char *str = (char *)malloc(100 * sizeof(char));
if (str == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return 1;
}
// Use the array (e.g., store a string)
snprintf(str, 100, "Hello, World!");
// Free the allocated memory
free(str);
str = NULL; // Prevent dangling pointer
return 0;
}
❌ Incorrect Usage
c
char str[100]; // Static array (no need to free)
free(str); // ❌ Error: Attempting to free a static array
char *str = "Hello"; // Pointer to a string literal
free(str); // ❌ Error: String literals are read-only and not dynamically allocated
🔍 Key Notes
Only free memory that was dynamically allocated with malloc, calloc, or realloc.
Always set the pointer to NULL after freeing it to prevent accidental reuse of the pointer.
Do not use free() on a pointer that was not allocated with these functions — it's undefined behavior and can cause crashes or memory corruption.
🔄 Reallocating or Resizing
If you use realloc to resize the array:
c
str = (char *)realloc(str, 200 * sizeof(char));
if (str == NULL) {
// Handle error
}
// Use the new array
free(str);
str = NULL;
In this case, free(str) is still used to release the memory.
✅ Summary
To safely free a char array pointer in C:
Ensure the pointer was dynamically allocated.
Call free(ptr).
Set ptr = NULL afterward.
This ensures proper memory management and avoids common pitfalls like memory leaks and dangling pointers.
User
If I char *buf = malloc(100);
Can the char[] be 100 characters long?
Assistant
Yes, a char *buf = malloc(100); can indeed represent a 100-character array, but the usage depends on how you intend to use it:
✅ Case 1: As a Raw Character Array (Not a String)
If you're using buf as a general-purpose array of characters (e.g., for binary data, arbitrary bytes, or non-null-terminated sequences), then:
You can safely store 100 characters in buf.
Each char takes 1 byte, and malloc(100) allocates exactly 100 bytes.
There's no need for a null terminator ('\0') in this case.
Example:
c
char *buf = malloc(100);
for (int i = 0; i < 100; i++) {
buf[i] = 'A' + (i % 26); // Fill with some characters
}
⚠️ Case 2: As a C-String (Null-Terminated)
If you're using buf as a C-style string, then:
You must leave room for the null terminator ('\0').
So the maximum number of characters you can store is 99 (leaving the 100th byte for '\0').
Example:
c
char *buf = malloc(100);
snprintf(buf, 100, "This is a string with 99 characters"); // Safe
// buf[99] will be '\0'
If you try to store 100 characters without a null terminator, you'll risk undefined behavior (e.g., when using functions like strlen, strcpy, etc.).
🔍 Summary
Usage Type
Maximum Characters
Notes
Raw character array
100
No null terminator needed
C-style string
99
Must leave room for '\0'
✅ Final Answer
Yes, char *buf = malloc(100); can represent a 100-character array if used as a raw array. However, if you're using it as a C string, you can only safely store 99 characters to allow for the null terminator. Always consider the context of your usage.