free
has no knowledge of \0
terminated strings.
free
will free what was allocated with malloc
, and should work properly in this situation.
If your evidence that free
is not working is simply that the string data still exists, then you misunderstand free
.
It does not zero out the memory. It simply marks it as available for use.
The original data remains in that memory. But the memory may be allocated by the next caller of malloc
, and that caller will be able to overwrite your data at will, because you don't own that data anymore!
If you want that memory cleared (such as, if it contains a password or a security key), you must clear it out with something like memset
, before you call free.
Again, free only marks the memory as "unallocated, available for use by malloc", and does not clear out the contents.
PS Some debugging systems, such as Visual Studio, will overwrite freed data, but only to make it obvious in the debugger that it has been freed. That behavior is not contractually needed in C, and only aids in debugging. Typically, freed memory may be filled with something like 0xdeadbeef
.