Re: Segmentation fault on end of loop... (getline)



"jan247" <an2king_kid@xxxxxxxxx> writes:
> hi, im new here... um, this code works except that, at the end of the
> loop, i always get a "Segmentation fault". Can anyone help me on this?
>
> using GNU C:
>
> ....
> FILE *fp;
> fp = fopen("./prg.c", "r");
>
> char *line;
> int x=1;
> int nbytes = 100;
> while(getline( &line, &nbytes, fp ) != -1)
> {
> printf("%d: %s", x, line );
> x++;
> }
> fclose(fp);
> ....

You call fopen(), but you don't check whether it succeeded.

getline() is not a standard C function.

<OT>
You might look into what getline() does if its first argument is a
pointer to an uninitialized pointer object (the value of line is
garbage).

You might also look at the type of the second argument.
</OT>

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Bug in SosMans getline_test
    ... is one of the official newsgroup programs. ... Well, it's listed on the group website, so I'd say that's official. ... GNU lib'c getline and Eric's getline evidently have different ... However what Ben said was "GNU libc's getline". ...
    (comp.lang.c)
  • Re: Segmentation fault on end of loop... (getline)
    ... >> using GNU C: ... > See what the others have said, plus, use fgets instead of getline. ...
    (comp.lang.c)
  • Re: Segmentation fault at malloc.c:4232
    ... the program doesn't segmentation fault. ... But the manual of getline() tells us to free the "*lineptr" when it is ...
    (Fedora)
  • Re: Segmentation fault at malloc.c:4232
    ... the program doesn't segmentation fault. ... But the manual of getline() tells us to free the "*lineptr" when it is ... it reports "double free or corruption" when doesn't comment line 27: ...
    (Fedora)