0

Hi im trying to read user input of "unlimited" length into an char array. It works fine for shorter strings, but for more than around 30 characters the programm crashes. Why does this happen and how can i fix this?

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

char* read_string_from_terminal()//reads a string of variable length and returns a pointer to it
{
    int length = 0; //counts number of characters
    char c; //holds last read character
    char *input;

    input = (char *) malloc(sizeof(char)); //Allocate initial memory

    if(input == NULL) //Fail if allocating of memory not possible
    {
        printf("Could not allocate memory!");
        exit(EXIT_FAILURE);
    }

    while((c = getchar()) != '\n') //until end of line
    {
        realloc(input, (sizeof(char))); //allocate more memory
        input[length++] = c; //save entered character
    }

    input[length] = '\0'; //add terminator
    return input;

}

int main()
{
    printf("Hello world!\n");
    char* input;
    printf("Input string, finish with Enter\n");
    input = read_string_from_terminal();
    printf("Output \n %s", input);
    return EXIT_SUCCESS;
}
5
  • 5
    realloc(input, (sizeof(char))); //allocate more memory This comment is wrong. Reallocating 1 byte to 1 byte is allocating no more memory. Ignoreing what is returned from realloc() is also bad. Commented Jun 15, 2016 at 15:05
  • 2
    Also don't forget to allocate space for the terminator. Commented Jun 15, 2016 at 15:07
  • This seems to work: realloc(input, (sizeof(char)*length+1)); Commented Jun 15, 2016 at 15:08
  • 1
    @t1msu This seems to work: realloc(input, (sizeof(char)*length+1)); And what do you assign the returned pointed to? Also, getchar() returns int, not char. Commented Jun 15, 2016 at 15:10
  • 1
    Do not cast the return value of malloc and friends. Commented Jun 15, 2016 at 15:36

1 Answer 1

1

realloc(input, (sizeof(char))); //allocate more memory only allocates 1 char. Not 1 more char. @MikeCAT

(sizeof(char)*length+1) is semantically wrong. Should be (sizeof(char)*(length+1)), but since sizeof (char) == 1, it makes no functional difference.

Need space for the null character. @MikeCAT

Should test for reallocation failure.

char c is insufficient to distinguish all 257 different responses from getchar(). Use int. getchar() may return EOF. @Andrew Henle

Minor: Better to use size_t for array indexes than int. int maybe too narrow.

In the end code needs to do something like:

size_t length = 0;
char *input = malloc(1);
assert(input); 
int c;
...
while((c = getchar()) != '\n' && c != EOF) {
  char *t = realloc(input, length + 1);
  assert(t); 
  input = t;
  input[length++] = c;
}
...
return input;

int main(void) {
  ...
  input = read_string_from_terminal();
  printf("Output \n %s", input);
  free(input);
  return EXIT_SUCCESS;
}    
Sign up to request clarification or add additional context in comments.

1 Comment

On a side note, code should return NULL is the very first getchar() returns EOF to indicate to the calling code end-of-file.

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.