|
From: David H. (D. Design) <dt...@dt...> - 2013-02-11 06:23:36
|
I am new to C and Valgrind, so I can't understand why I am getting an error
message on what seems like a pretty standard if statement.
Basically I want to strip the newline character, '\n', from the value fgets
returns to me. Pretty much everywhere I could find said the recommended way
to do this was something like this: (getInput is a function I have written
where fgets resides and it does some validation)
char *getInputNoNewline(int max, char message[]) { static char* input; int
maxchars = max+ADDCHARS input = (char*)malloc(maxchars); input =
getInput(max, message); if (input[strlen(input) - 1] == '\n') {
input[strlen(input) - 1] = '\0'; } return input; }
The if statement is giving me 57 errors from 6 contexts.
Running in verbose mode a typical error is:
==18533== 3 errors in context 3 of 18:
==18533== Invalid read of size 1
==18533== at 0x100000BF0: getInputNoNewline (start.c:44)
==18533== by 0x100000D2A: main (start.c:61)
==18533== Address 0x10027f434 is 4 bytes inside a block of size 6 free'd
==18533== at 0x10000FE9F: free (vg_replace_malloc.c:366)
==18533== by 0x100000B88: getInput (start.c:35)
==18533== by 0x100000BCD: getInputNoNewline (start.c:43)
==18533== by 0x100000D2A: main (start.c:61)
The statement replacing '\n' with '\o' is giving me 75 errors from 12
contexts.
Typical error message:
==18533== 18 errors in context 17 of 18:
==18533== Invalid read of size 1
==18533== at 0x100011503: strlen (mc_replace_strmem.c:282)
==18533== by 0x100000C0D: getInputNoNewline (start.c:45)
==18533== by 0x100000CF8: main (start.c:59)
==18533== Address 0x10027f361 is 1 bytes inside a block of size 50 free'd
==18533== at 0x10000FE9F: free (vg_replace_malloc.c:366)
==18533== by 0x100000B88: getInput (start.c:35)
==18533== by 0x100000BCD: getInputNoNewline (start.c:43)
==18533== by 0x100000CF8: main (start.c:59)
Are these errors that valgrind is giving me likely to cause any errors down
the track as programs become more complex?
|