Question

I'm not very professional on C and so I get a problem with valgrind.

I want to read in an input from command-line.

If I try it this way it works fine.

//call the function
char *command = getUserInput();


//function
char *getUserInput()
{ 
  char *buffer = NULL;
  char *temp = NULL;
  unsigned int count = 0;
  unsigned int lenght = 10;
  char character = 0;

  buffer = malloc((lenght+1)*sizeof(char));
  if(buffer == NULL)
  {
    // printf(ERROR_OUT_OF_MEM);
    // return EXIT_OUT_OF_MEM;
  }

  while((character = getchar()) != '\n')
  {
    if(count == lenght)
    {
      lenght += 10;
      temp = realloc(buffer,lenght*sizeof(char));
      if(temp != NULL)
        {
          buffer = temp;
        }
      else
        {
          free (buffer);
          // printf(ERROR_OUT_OF_MEM);
          // return EXIT_OUT_OF_MEM; 
        }
    }

    buffer[count] = character;
    count++;

  }
  buffer[count] = '\0';

  return buffer;
}

But I cant geht an error-returnvalue.

If I try it in the way by call-by-reference i get some valgrind error I don't understand. And I know, in this example-function-call I don't request the return-value.

//call function
char *command = NULL;
getUserInput(command);

//function
int getUserInput(char *name)
{ 
  char *temp = NULL;
  unsigned int count = 0;
  unsigned int lenght = 10;
  char character = 0;

  name = malloc((lenght+1)*sizeof(char));
  checkMemory(name);

  while((character = getchar()) != '\n')
  {
    if(count == lenght)
    {
      lenght += 10;
      temp = realloc(name,lenght*sizeof(char));
      if(temp != NULL)
        {
          name = temp;
        }
      else
        {
          free (name);
          printf(ERROR_OUT_OF_MEMORY_MESSAGE);
          return ERROR_OUT_OF_MEMORY; 
        }
    }

    name[count] = character;
    count++;

  }
  name[count] = '\0';

  return RETURN_SUCCESS;
}

Commandhandler-Line 1199 is if (strcmp(command, "thing_to_compare") == 0)

Valgrind:

==23886== Use of uninitialised value of size 4
==23886==    at 0x40256BB: strcmp (mc_replace_strmem.c:426)
==23886==    by 0x8049B39: commandHandler (assa.c:1199)
==23886==    by 0x8049D6C: main (assa.c:1295)
==23886==  Uninitialised value was created by a stack allocation
==23886==    at 0x8049B00: commandHandler (assa.c:1189)
==23886== 
==23886== Invalid read of size 1
==23886==    at 0x40256BB: strcmp (mc_replace_strmem.c:426)
==23886==    by 0x8049B39: commandHandler (assa.c:1199)
==23886==    by 0x8049D6C: main (assa.c:1295)
==23886==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

I totally don't know where the problem is.

kind regards Philipp

Was it helpful?

Solution

getUserInput has no way to change the value of command, as all functions are pass-by-value in C. Therefore, when you get to all your strcmp functions, you are dereferencing that NULL pointer causing all sorts of bad things.

If you want getUserInput to be able to change the value of command, you must pass the address of command to the function (which means the function must then take a char** argument).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top