0

i want to repair one error..

Valgrind says me this:

==9203== 1 errors in context 1 of 1:
==9203== Conditional jump or move depends on uninitialised value(s)
==9203==    at 0x4C2D64A: strncat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9203==    by 0x400970: newSpeak (main.c:39)
==9203==    by 0x400A62: main (main.c:74)
==9203== 
--9203-- 
--9203-- used_suppression:      2 dl-hack3-cond-1
==9203== 
==9203== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

Here is my function newSpeak()

int velikost = 0, i = 0, delka = 0;
char * textNovy = NULL;

i = 0;
    while (text[i] != '\0') {
        delka++;
        i++;
    }

textNovy = (char*)malloc(sizeof(char));
    for (i = 0; i < delka; i++) {
        textNovy = (char*)realloc(textNovy, ((i+1)+velikost)*sizeof(char) );
        strncat(textNovy, text+i, 1);
    }
return textNovy;

value text is given to function from main. Problem is somewhere in strncat

Thans you!! Lukas

  • In `i < delka`, I suspect `delka` is not initialized. –  Nov 30 '13 at 17:54
  • Do what valgrind says: use `--track-origins=yes` and you'll understand where the problem probably comes from. – Cahu Nov 30 '13 at 18:08
  • @H2C03 no it's initialized at the top and it doesn't even match the line of the error. – Xonar Nov 30 '13 at 18:09
  • `double * textNovy` **really** should be `char * textNovy`! – alk Nov 30 '13 at 18:12
  • it matches in strncat - I removed some unimportant lines of code. In strncat is something unitialized, it says. – user3052695 Nov 30 '13 at 18:14
  • @user3052695 Just a suggestion, but rather use strlen to get the string length, don't use a double pointer for text, velikost is just 0 why not use a const, and are you sure the for loop is doing what you want it to do? – Xonar Nov 30 '13 at 18:15
  • sorry for double/char - I had it only here. Repaired. But it is still same – user3052695 Nov 30 '13 at 18:15
  • Why are you concatenating to the string one letter at a time? – Xonar Nov 30 '13 at 18:17
  • Jonathan Leffler is right! I have to set some value in textNovy. Thanks to you all! It works now. – user3052695 Nov 30 '13 at 18:19

1 Answers1

4

You never initialize the contents of textNovy, yet you concatenate on to the end of it. This leads to the error you are seeing from valgrind.

You need at least:

textNovy[0] = '\0';

(or an equivalent) after the malloc().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    maybe a memset to set the range of memory – ojblass Nov 30 '13 at 18:12
  • @ojblass: you could use `memset()`, but there's no need; the `strncat()` will set what needs to be set as long as it has a null byte at the start on the first time it is called. – Jonathan Leffler Nov 30 '13 at 18:33
  • @user3052695 Why would you allocate a string as small as a `char` ? –  Nov 30 '13 at 18:42
  • @user9000: I normally wouldn't allocate a single character, but the code does; it is sufficient to get the process started, and isn't formally wrong. The `realloc()` code increases the space so that (presumably) there is enough space for whatever needs to be stored. – Jonathan Leffler Nov 30 '13 at 18:45
  • Ah, didn't notice that, sorry. –  Nov 30 '13 at 18:47