-1

I need to realloc a string acquired via scanf("%ms", ...), does realloc automatically include the termination character \0 in my reallocated string? What's the behavior of reallocin this case?

Will it add \0 at the end of the reallocated string, or will it leave the \0 in the same position of the previous string, adding uninitialized memory after \0?

For example:

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

int main() {
    char *string = NULL;
    char *p = NULL;
    int length = 0;
    //This should automatically add \0 at the end, if i'm not wrong
    scanf("%ms", &string);
    length = strlen(string);
    p = realloc(string, sizeof(char) * (length + 10));
    if (p != NULL) {
       string = p;
       p = NULL;
    }
    free(string);
    return 0
}

PS: I used strlen() over the string like here: strlen with scanf("%ms"...)

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49

3 Answers3

3

realloc doesn't know/care about null bytes or whatever else is stored in the given object. It simply guarantees that the old content is preserved/copied to in the new object it returns (assuming the realloc call succeeds). As long you have previously added it, it'll be there after realloc too. And in your case, null byte's there (assuming scanf succeeded), so it'll be there after realloc too.

However, note that if you shrink the object with realloc, then only the contents up to the specified size will be preserved - in this case, you may not have the null byte after realloc.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
P.P
  • 117,907
  • 20
  • 175
  • 238
2

realloc() is not just for characters or integers. It will free the previous memory allocation automatically and then reallocate the requested memory.

Will it add \0 at the end of the reallocated string?

It's out of the question.

Will it leave the \0 in the same position of the previous string, adding uninitialized memory after \0?

realloc() doesn't overwrite old contents, not prior memory place. It doesn't touch its content, just moves to and reallocates new memory chunk(s).

0

realloc() - Changes the size of the memory block pointed to by ptr.

The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. If the new size is larger, the value of the newly allocated portion is indeterminate.

You need to allocate the memory for string, using malloc() before using realloc(). scanf() will fail to write memory to NULL pointer (string value).