Is null character included while allocating using malloc

If you need that block for storing null-terminated string then yes, you need to explictly ask malloc() to allocate an extra byte for storing the null-terminator, malloc() will not do it for you otherwise. If you intend to store the string length somewhere else and so you don’t need the null terminator you can get away without allocating the extra byte. Of course it’s up to you whether you need null-termination for strings, just don’t forget that C library string handling functions only work with null-terminated strings.

Leave a Comment