c - Valgrind-Uninitialised value error with getline() function -
i want read multiple lines text file loop, conditional jump or move depends on uninitialised value(s)
error in getline()
line.
my code:
char *string; size_t len = 0; while (getline(&string, &len, filestream) != -1) { // error happens line // } free(string); fclose(filesream);
i tried failed fix it. solutions appreciated.
you need either of below.
set
char *string = null;
,len
0
. [[ preferred method ]]allocate memory
char *string
, send size of allocated memory usinglen
.
related quotes man page referrence
if *lineptr set null , *n set 0 before call, getline() allocate buffer storing line. buffer should freed user program if getline() failed. alternatively, before calling getline(), *lineptr can contain pointer malloc(3)-allocated buffer *n bytes in size. if buffer not large enough hold line, getline() resizes realloc(3), updating *lineptr , *n necessary.
Comments
Post a Comment