0

I am trying to allocate string of unknown length. I have made this program to do so, it works fine, but when I try valgrind it always shows 1 error the "Conditional jump or move depends on uninitialised value(s)" The program looks something like this

char *text = NULL;
int i = 0, error = 0;
text = (*char) malloc(sizeof(char));
while(1){
   error = scanf("%c", &text[i]);
   if(error == EOF)
      break;
   i++;
   text = (*char) realloc(text, sizeof(char) * (i + 1));
}
printf("%s", text);
free(text);

I have tried using

scanf("%ms", &text);

but this stops, when a new line character appears, which is a problem in my task. Any suggestions ?

  • 3
    `(*char)` should be `(char*)`. But [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Nov 21 '19 at 18:33
  • 3
    The code you show has one major problem: You forgetting that strings need to be *null-terminated*. You need to allocate one extra character and before you print the string you need to initialize it to `'\0'`. – Some programmer dude Nov 21 '19 at 18:33
  • 1
    Also please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And learn how to create a [mcve] to show us. It's not really enough that you show us "something like" the code you have, better show us a proper example which replicates the problem you ask about, and doesn't contain any other unrelated errors. – Some programmer dude Nov 21 '19 at 18:35
  • 2
    This approach of "try and jam the string in the buffer, and if that doesn't work, increase the length by *one* and try again" is really inefficient. Try increasing the size of the buffer by double, or perhaps 1.5x each round. – tadman Nov 21 '19 at 18:36
  • Regarding newline, the code you show shouldn't stop on newline. It will read input from standard input until you actually input an "end of file" (e.g. `Ctrl-D` on POSIX systems like Linux or macOS, or `Ctrl-Z` on an empty line on Windows). – Some programmer dude Nov 21 '19 at 18:37
  • Thank you so much ! Initializing the last character of the string to '\0' worked :) – bushido Nov 21 '19 at 18:48

1 Answers1

0

Alternative for writing explicit loop to getchar/realloc:

scanf will read until a white space. Few alternatives to perform multi-line read, using standard library calls. Fewer lines/fewer bugs.

Using getdelim, with a 0 delim. Will work as long as the input does not have NUL characters.

    char *cp = NULL ;
    size_t n = 0 ;
    getdelim(&cp, &n, 0, stdin) ;
    printf("S=%s\n", cp) ;

If getdelim is not available, possible to use character sequence that will effectively accept all characters. Using a char sequence that will reject character that is not going to show in the input (e.g., Ctrl/A, '\001').

    char *cp = NULL ;
    fscanf("%m[^\1]", &cp) ;
    printf("S=%s\n", cp) ;
dash-o
  • 13,723
  • 1
  • 10
  • 37