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

Discussion in 'C Programming' started by jan247, Dec 27, 2005.

  1. jan247

    jan247 Guest

    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);
    .....
     
    jan247, Dec 27, 2005
    #1
    1. Advertising

  2. jan247 wrote:
    -snip-
    > 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);

    -snip-

    You have to allocate memory for `line' before you can use getline:

    line = malloc(nbytes + 1);
    ...


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
     
    August Karlstrom, Dec 27, 2005
    #2
    1. Advertising

  3. "jan247" <> 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) <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.
     
    Keith Thompson, Dec 27, 2005
    #3
  4. jan247

    pemo Guest

    "jan247" <> wrote in message
    news:...
    > 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);
    > ....


    See what the others have said, plus, use fgets instead of getline.
     
    pemo, Dec 27, 2005
    #4
  5. pemo said:

    > See what the others have said, plus, use fgets instead of getline.


    The two do not perform the same task. The getline function does a job which
    no single standard library function can do. On the other hand, it isn't a
    standard ISO C function. The decision about which to use is not as
    clear-cut as you make it sound.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 27, 2005
    #5
  6. "pemo" <> writes:
    > "jan247" <> wrote in message
    > news:...
    >> 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:
    >>
    >> ....

    [snip]
    >> while(getline( &line, &nbytes, fp ) != -1)

    [snip]
    >
    > See what the others have said, plus, use fgets instead of getline.


    fgets() is standard, while getline() is a GNU extension, so that's
    good advice if you're concerned about portability. However, getline()
    does have some advantages over fgets() (I won't go into the details),
    so it's perfectly appropriate to use it if portability to non-GNU
    implementations isn't a concern.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Dec 27, 2005
    #6
  7. jan247 wrote:
    > 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");

    You must check the value of fp here.
    > char *line;
    > int x=1;
    > int nbytes = 100;
    > while(getline( &line, &nbytes, fp ) != -1)
    > {
    > printf("%d: %s", x, line );
    > x++;
    > }
    > fclose(fp);


    getline is not a standard C function; it is a GNU extension,
    but it is not too difficult to implement it in terms of standard
    functions.

    <OT>
    You should set the line pointer to NULL, or a value returned from
    a call to malloc, before the first call to getline. For example;
    size_t nbytes = 0;
    char *line = NULL;
    or alternatively,
    size_t nbytes = 100; /* any value should work */
    char *line = malloc(nbytes);

    Read manual page of getline for the details.
    </OT>
     
    M. Nejat AYDIN, Dec 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    871
  2. Pud
    Replies:
    0
    Views:
    578
  3. Replies:
    0
    Views:
    532
  4. Replies:
    7
    Views:
    1,189
    Jorgen Grahn
    May 2, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page