473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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);
.....

Dec 27 '05 #1
6 4887
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.
Dec 27 '05 #2
"jan247" <an*********@yahoo.com> 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) ks***@mib.org <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.
Dec 27 '05 #3

"jan247" <an*********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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.
Dec 27 '05 #4
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)
Dec 27 '05 #5
"pemo" <us***********@gmail.com> writes:
"jan247" <an*********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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) ks***@mib.org <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.
Dec 27 '05 #6
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>
Dec 27 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
6
by: AMT2K5 | last post by:
Hello guys. I have a function, cleanSpace . I was told from another source that the problem is, is that the function is acting on constant string literal. To fix this I was told to take the...
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
1
by: jwlkr | last post by:
Hi, I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
6
drumgirl67
by: drumgirl67 | last post by:
I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.