0

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?

4
  • 1
    Of course, there is a nul-terminator in the string. scanf puts it there after the last input character. Otherwise it would not be a string in C at all. That realloc call can only be used to reduce the size of the buffer. It cannot increase it that way. Commented Oct 25, 2023 at 10:44
  • 1
    If there's no null-terminator, it's not a null-terminated string. And you can't pass it to any function expecting a null-terminated string. All those functions will go out of bounds and have undefined behavior. Commented Oct 25, 2023 at 10:45
  • Unrelated to your question, this is some pretty bad code in terms of execution speed. It would be better without the realloc. Commented Oct 25, 2023 at 11:15
  • OT: Why does the HackerRank starter code use #include <math.h> for this text processing challenge? Commented Oct 25, 2023 at 21:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.