Is there a native C function to free a NULL termianated char array?
- User
Is there a native C function to free a NULL termianated char array?
Assistant
Yes, you can use
free()to deallocate a null-terminated char array, but only if it was allocated withmalloc(),calloc(), orrealloc().cchar *str = malloc(100); // Allocate memory // ... use the string ... free(str); // Free the memory str = NULL; // Good practice to avoid dangling pointerHowever, you cannot use
free()on:- String literals:
char *str = "hello"; - Stack-allocated arrays:
char str[100]; - Static arrays
If you're working with an array of strings (char**), you'd need to free each individual string and then the array itself:
- String literals:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.