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

int main(void) {
  int i = 0;
  char c, *input;
  input = (char *) malloc(sizeof(char));

  if(input == NULL) {
    printf("NOT ENOUGH SPACE!");
    exit(1);
  }

  printf("Input a string, press ENTER when done: ");

  while((c = getchar()) != '\n') {
    realloc(input, (sizeof(char)));
    input[i++] = c;
  }

  input[i] = '\0';
  printf("\nYou've entered the string: %s\n", input);
}

The above code snippet works well for small input. But it fails whenever the input provided is of large size. Either there is a run time error or a segmentation error. There may be some error in reallocation of memory space. I basically want to store a character array dynamically from user, i.e without mentioning the capacity of the input the user can directly put in any size of character array.

1 Answer 1

2

The logic here is wrong:

  while((c = getchar()) != '\n') {
      realloc(input, (sizeof(char)));
      input[i++] = c;
  }

You're not actually increasing the size of the buffer, and you are also discarding the result of realloc.

Try:

  while ((c = getchar()) != '\n') {
    // Note: you need one extra character for the terminator, so for the
    // first char, when `i` is 0, then you need room for two `char`s in
    // the buffer - one for the first input character and one for the
    // terminator. And so on...
    char * temp = realloc(input, i + 2); // NB: realloc can, and sometimes does, fail
    if (temp == NULL) // if realloc failed then exit program
        exit(1);
    input = temp;     // otherwise update input...
    input[i++] = c;
  }


Also, since you are always going to be calling realloc on every character (which is very inefficient, incidentally, but it works), this line:

input = (char *) malloc(sizeof(char));

(which should not have a cast, BTW, since this is C, not C++) can just be:

input = NULL;


And one final bug:

char c;

should be:

int c;

otherwise your while loop may never terminate, since EOF can only be properly represented as an int.


So the final fixed program should look something like this:

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

int main(void) {
    int i = 0;
    int c;
    char * input = NULL;

    printf("Input a string, press ENTER when done: ");

    while ((c = getchar()) != '\n') {
        // Note: you need one extra character for the terminator, so for the
        // first char, when `i` is 0, then you need room for two `char`s in
        // the buffer - one for the first input character and one for the
        // terminator. And so on...
        char * temp = realloc(input, i + 2); // NB: realloc can, and sometimes does, fail
        if (temp == NULL) // if realloc failed then exit program
            exit(1);
        input = temp;     // otherwise update input...
        input[i++] = c;
    }

    input[i] = '\0';
    printf("\nYou've entered the string: %s\n", input);

    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

That is great sir. But I am still getting a TLE. After replacing this snippet while((c = getchar()) != '\n') { _ realloc(input, (sizeof(char)));_ _ input[i++] = c;_ } with yours.
Did you apply all THREE bug fixes above ?
No basically I applied only the one in the while loop. Should I apply all of them?
Um, think about it a minute - you have (at least) three bugs in your code - should you just fix one of the bugs, or should you fix all three ? (I've now added a complete, fixed, version of the code, above, for your convenience.)
Well. All three. :D Yea. Now it's running smooth. Thank You Sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.