Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segmentation fault on end of loop... (getline)

Reply
Thread Tools

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

 
 
jan247
Guest
Posts: n/a
 
      12-27-2005
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);
.....

 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      12-27-2005
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-27-2005
"jan247" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      12-27-2005

"jan247" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-27-2005
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)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-27-2005
"pemo" <(E-Mail Removed)> writes:
> "jan247" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
M. Nejat AYDIN
Guest
Posts: n/a
 
      12-27-2005
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments