cmemoryreallocstrlen

Why +1 in realloc for string in C? If it's for null terminator, then how does strlen() measures len of the str if there's no null byte at the end?


This code is from a problem in HackerRank (Q: Printing Tokens in C).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char *s = malloc(1024 * sizeof(char));  // Allocate for string
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s)+1);   // Here's my doubt

    // Solution
    
    return 0;
}

Forget about the question, look at the following iteration-

s = realloc( s, strlen(s)+1 );   // +1

This line of code reallocates the memory of the string 's' according to the length of the string 's', It also increases the length for a null terminator '\0'. But strlen() only measures until it reaches '\0', but there's no '\0' in 's' as we're adding a byte for '\0'. How can we assume it will measure the perfect length of string 's'? If it measures the perfect length of 's' then there must be a null terminator at the end so why add 1 again?

Someone explain this, there's no null terminator '\0' so we add a byte for it.

Or Am I completely wrong about this?


Solution

  • Someone explain this, there's no null terminator '\0' so we add a byte for it.

    You are misinterpreting what happens here.

    scanf is used to read a string into the memory pointed to by s. In C, a string is a nul-terminated sequence of characters. That means, it is responsibility of scanf to place the 0 byte there after the last character from the input.

    If that weren't the case, you would not be allowed to pass s to strlen at all because it expects a nul-terminated string.

    In realloc the +1 is not added to add a 0 byte. It is added to keep the memory of that byte. You start with a large buffer and resize to the length you need. For this, you must add 1 to prevent the last byte from being freed during realloc.

    So, the assumption that there would not be a nul-terminator, is wrong. It is there all the time and shall still be there after reducing the buffer size.