2

I am a newbie in C and for an exercise I have to write a program, where I can read in strings. If my reserved memory (length BUFFER_SIZE) isn't enough, it should reserve memory in increments of +=BUFFER_SIZE, as long as needed to read the string. I tried to write some functions to get this done, but it doesn't work. Can somebody please help me?

My code:

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

#define BUFFER_SIZE 10

size_t string_length(char *string)
{
  size_t i = 0;
  while(string[i] != '\0')
  {
    i++;
  }
  return i;
}
void string_concatenate(char *string, char *string_to_chain)
{
  size_t length = string_length(string);
  for(size_t i = 0; *(string_to_chain + i) != '\0'; i++, length++)
  {
    string[length] = string_to_chain [i];
  }
  string[length] = '\0';
}
char *string_search(char *string, char character)
{
  do
  {
    if (*string == character)
      {
        return (char*)string;
      }
  } while (*string++);
  return NULL;
}
char *get_line()
{
  int line_size = BUFFER_SIZE;
  char* line = malloc(line_size * sizeof(char));
  if(line == NULL)
  {
    return NULL;
  }
  printf("Bitte Text eingeben: \n");
  fgets(line, BUFFER_SIZE, stdin);

  char *new_line_character = string_search(line, '\n');
  while(new_line_character == NULL)
  {
    line_size += BUFFER_SIZE;

    line = realloc(line, (line_size * sizeof(char)));
    if(line == NULL)
    {
      return NULL;
    }

    char *new_line = line + BUFFER_SIZE - 1;
    fgets(new_line, line_size, stdin);
    new_line_character = string_search(line, '\n');
  }
  
  *new_line_character = '\0';
  return line;
  
}
}
int main(void) {
    char *string = get_line();
    printf("s%\n", string); 
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
c_noob
  • 21
  • 2
  • 4
    With your `realloc` call, if it fails it will leave the original memory alone. Since you reassign back to `line` you loose the original `line` and have a memory leak. – Some programmer dude Dec 16 '21 at 08:28
  • 4
    Also, in C you don't have to [cast the result of `malloc` (or realloc`)](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Dec 16 '21 at 08:29
  • 2
    There are also other problems, like `string_concatenate(line, new_line)` which should not be needed since you already use `fgets` to read into the existing buffer. – Some programmer dude Dec 16 '21 at 08:29
  • 2
    `*string_to_chain+i != '\0'` ==> `string_to_chain[i] != '\0'` or `*(string_to_chain+i) != '\0'` – mch Dec 16 '21 at 08:31
  • 1
    `printf("%\n", string);` ought to create problems on the stack. It should be `printf("%s\n", string);` – Ted Lyngmo Dec 16 '21 at 08:44
  • @TedLyngmo What problems do you expect? The second argument is simply ignored... – the busybee Dec 16 '21 at 09:59
  • @thebusybee I expect a `char*` to be placed on the stack and not being pop:ed from it, wasting a full pointer before the program terminates anyway :-) If repeated many (many) times, it'd eat up the allocated stack memory though. – Ted Lyngmo Dec 16 '21 at 10:02
  • @TedLyngmo Then your implementation of `printf()` is erroneous, or your compiler. I never have seen this in nearly 40 years of programming C. Would you mind to tell us which compiler system does this? – the busybee Dec 16 '21 at 10:08
  • @thebusybee Must have been an Ultrix compiler in the 80's. :-) It did not parse the format string to discard the varargs if they didn't match up. – Ted Lyngmo Dec 16 '21 at 10:13
  • thank you! now i edited my code. it works until the third extension to my meomory. than my compiler write: realloc(): invalid next size Aborted (core dumped) What should i do? sry for my bad skills.. @Someprogrammerdude @ all – c_noob Dec 16 '21 at 10:47
  • 1
    Very likely you need to allocate one more byte in all your allocations, or you need to read one less byte with `fgets`. The null-terminator might be written out of bounds. You should probably take this opportunity to learn how to use a *debugger* to catch crashes in your code, and how to use it to locate when and where in your code the crash happens, and how to go to that location and examine all involved variables and their values at the time of the crash. – Some programmer dude Dec 16 '21 at 10:58
  • @TedLyngmo To use functions with a variable number of arguments with that compiler must be really a PITA. The developers would have to make sure that they implement the correct number, sequence and types of `va_arg()` for each different call. Oh-oh. However, these times are long gone. Please don't frighten young developers with such war stories. ;-) – the busybee Dec 16 '21 at 10:58
  • @thebusybee I'll try to refrain from it :-) Yeah, it was hard back then ... and if you tell that to young people today, they won't believe you ... – Ted Lyngmo Dec 16 '21 at 11:04

0 Answers0