0

on googling i got an answer but i am not able to understand how it works. I have just started coding so i hope someone could explain the code clearly for me.

char *s;
s = malloc(1024 * sizeof(char));
scanf("%s", s);
s = realloc(s, strlen(s) + 1);

Thank you in advance.

  • Welcome to StackOverflow! Please take the [tour] to learn how this site works. -- Questioners should put some effort in their issues. Did you look up the documentation of `malloc()` and `realloc()`? Why did it not help you? What other insights did you have when reading introductory tutorials on the matter? What specifically is not clear? – the busybee Aug 28 '22 at 13:41
  • This is terrible practice. You should be wary of any code samples you get from this source. – William Pursell Aug 28 '22 at 13:42

2 Answers2

4
char *s;       
s = malloc(1024 * sizeof(char));

The above declares the variable s and assigns to it the address of a dynamically allocated chunk of memory large enough to hold 1024 chars. However, no error checking is performed, so s may be NULL at this point.

scanf("%s", s);

This reads data from the input stream until a whitespace is encountered (or input terminates) and stores it in the allocated chunk of memory whose address is contained in s , appending a '\0' after the data that is read. If there is more than 1023 bytes of data or if the previous malloc returned NULL, undefined behavior occurs. (This is bad.)

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

This is an attempt to shrink the amount of memory being used so that s now points to a smaller chunk of memory which is just big enough to hold the string that was read. The +1 is in the 2nd argument to realloc because C stores strings as null terminated arrays, and it takes an extra character to store that terminator. This is possibly a memory leak, since realloc can return NULL, in which case the original memory is lost and the program hasn't stored that value anywhere so cannot free it.

Do not ever use "%s" in a call to scanf. If you have allocated N bytes for an array, use a width modifier of N - 1. Often this means dynamically generating the format string, but often it is as simple as:

char s[32]; scanf("%31s", s); 

Always check the value returned by malloc, realloc, and scanf.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
0

An alternative approach to shrinking the memory to the minimum needed for user input would be to allocate a second chunk of memory based on the size of the input, copy the string into it, then free the original.

We're just going to assume malloc and scanf worked as we meant them tto and not error check for the purposes of this answer.

char *s1 = malloc(1024);
scanf("%1023s", s1);

char *s2 = malloc(strlen(s1) + 1);
strcpy(s2, s1);

free(s1);

You might also use strdup.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Instead of creating a new string and copying the contents of the old string into it can't we just reallocate the size of the old string. `realloc(s1,strlen(s1)+1);` will do the job right or are there any problems in doing so? – abhiram reddy Aug 29 '22 at 12:50