Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Good C resources? Reading line-by-line segfault...
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
OdinsDream
Veteran
Veteran


Joined: 01 Jun 2002
Posts: 1057

PostPosted: Tue Sep 16, 2003 2:00 pm    Post subject: Good C resources? Reading line-by-line segfault... Reply with quote

Does anyone have some staple online resources for the C language? It's a bit trying to enter any single-letter search terms in google. So far I've been using cplusplus.com, and the "Application Programming in ANSI C" book.

I keep getting a segmentation fault on this:

Code:

   size_t len = 0;printf("Setting up variables for line-by-line read.\n");
   char * line = NULL;
   size_t len = 0;
   ssize_t read;
   printf("Done.\n");
   
   while((read = getline(&line, &len, dictFile)) != -1) {
      printf("Storing: %s", line);
      //storeWord(line, root, 0);
   }
   printf("Done.\n");


The output being:
Code:

Setting up variables for line-by-line read.
Done.
Segmentation fault


I'm using the same input file, which is puzzling because this worked fine awhile ago, and I haven't changed this code section...

Is there a better way to do this?
_________________
s/(?<!gnu\/)linux(?! kernel)/GNU\/Linux/gi

Don't blame me. I didn't vote for him.

http://john.simplykiwi.com
Back to top
View user's profile Send private message
cederberg
Guru
Guru


Joined: 23 Jan 2003
Posts: 349
Location: Stockholm / Sweden

PostPosted: Tue Sep 16, 2003 2:46 pm    Post subject: Re: Good C resources? Reading line-by-line segfault... Reply with quote

OdinsDream wrote:
Code:

   size_t len = 0;printf("Setting up variables for line-by-line read.\n");
   char * line = NULL;
   size_t len = 0;
   ssize_t read;
   printf("Done.\n");
   
   while((read = getline(&line, &len, dictFile)) != -1) {
      printf("Storing: %s", line);
      //storeWord(line, root, 0);
   }
   printf("Done.\n");

Normally SIGSEGVs (segmentation faults) are caused by something pointing to Rome (most frequently NULL).

In your case it could be the "dictFile" variable, or the fact that you do not reinitialize the "line" variable to NULL after storing the line.

The "man" command is often an excellent tool when programming in C. See "man getline" for example.
Back to top
View user's profile Send private message
OdinsDream
Veteran
Veteran


Joined: 01 Jun 2002
Posts: 1057

PostPosted: Tue Sep 16, 2003 4:17 pm    Post subject: Reply with quote

It seems there's a completely unrelated section of the code that affects the result of getline.

Directly before the getline "while" loop, I have created a pointer to a node. Like so:

Code:

// Create the dictionary trie
   node* root = (node *) calloc(1, sizeof(node *)); //root node. Only a placeholder pointing to nodes.
   if( root == NULL ) {
      fprintf(stderr, "Error: Could not allocate memory for the root node.");
      exit(1);
   }
   /* printf("Setting content of Root Node.\n");
   root->content = '\0'; //This node is bogus. Ensure the content never matches real letters.
   printf("Setting nextNode of Root Node.\n");
   root->nextNode = NULL;
   printf("Setting subNode of Root Node\n");
   root->subNode = NULL;
   printf("Setting isWord of Root Node.\n");
   root->isWord = 0;
   printf("Done.\n"); */
   
   printf("Setting up variables for line-by-line read.\n");
   char * line = NULL;
   size_t len = 0;
   ssize_t read;
   printf("Done.\n");


After this, the while loop is executed. As it is above, the loop runs successfully. Notice the sections commented out.

With these sections uncommented, however, the getline call causes a Segmentation Fault. What have I trampled on here? Have I misused calloc() above to create a single new node?

Edit:

Arg, answered my own question. calloc() should use sizeof(node), rather than sizeof(node *). That'll teach me to copy-paste... :roll:
_________________
s/(?<!gnu\/)linux(?! kernel)/GNU\/Linux/gi

Don't blame me. I didn't vote for him.

http://john.simplykiwi.com
Back to top
View user's profile Send private message
cederberg
Guru
Guru


Joined: 23 Jan 2003
Posts: 349
Location: Stockholm / Sweden

PostPosted: Tue Sep 16, 2003 4:36 pm    Post subject: Reply with quote

Weird though that you got the printf:s from the code below printed. I guess you must have executed that piece of code twice, or your compiler made some really agressive optimizations...
Back to top
View user's profile Send private message
ed0n
l33t
l33t


Joined: 23 Apr 2003
Posts: 638
Location: Prishtine/Kosove

PostPosted: Tue Sep 16, 2003 4:46 pm    Post subject: Reply with quote

I am a n00b in C I just started learning it I got segfault on this

Code:

printf("Ti e ki emrin : ");
scanf("%s", &emri);
fajlli = fopen(e,"w");

fprintf(fajlli,"\n%s\n%s\n", vijat, emri);

fclose(fajlli);


and the output is this:

Code:

ed0n@ed0n teste $ ./outputeee -i
Ti e ki emrin : e
Segmentation fault
ed0n@ed0n teste $


-ed0n
Back to top
View user's profile Send private message
cederberg
Guru
Guru


Joined: 23 Jan 2003
Posts: 349
Location: Stockholm / Sweden

PostPosted: Tue Sep 16, 2003 5:02 pm    Post subject: Reply with quote

ed0n wrote:
scanf("%s", &emri);

You have initialized the "emri" variable before this, right? I think something like "char emri[4096]" would be adequate, but that depends on the input you expect.
Back to top
View user's profile Send private message
ed0n
l33t
l33t


Joined: 23 Apr 2003
Posts: 638
Location: Prishtine/Kosove

PostPosted: Wed Sep 17, 2003 11:18 am    Post subject: Reply with quote

cederberg wrote:
ed0n wrote:
scanf("%s", &emri);

You have initialized the "emri" variable before this, right? I think something like "char emri[4096]" would be adequate, but that depends on the input you expect.


yes I did it .
Back to top
View user's profile Send private message
Ox-
Guru
Guru


Joined: 19 Jun 2003
Posts: 305

PostPosted: Wed Sep 17, 2003 2:42 pm    Post subject: Reply with quote

OdinsDream, I don't know the getline function, but I'm guessing your problem is a common C programming mistake.

You need to read about exactly how pointers and memory buffers work, and the differences between them.

To quickly explain from your example:
Code:
char *line = NULL
This makes a new variable that is a pointer (not a buffer), and initializes it to point to nothing (NULL).

Code:
read = getline(&line, &len, dictFile)
Here, the getline function needs a buffer to put the string. This means it needs a pointer to a buffer. What you are passing is a pointer to a pointer (&line). That's why you get a sigsegv... there is no buffer.

Either point line to a valid buffer and just pass line (not &line) like this:
Code:
char *line;
size_t len = 0;
line = malloc(80); /* dynamically allocate a buffer */
... getline(line,&len,...

Or change line to be a real buffer and pass its address to getline:
Code:
char line[80]; /* staticly allocate a buffer */
size_t len = 0;
... getline(&line,&len,...
Back to top
View user's profile Send private message
cederberg
Guru
Guru


Joined: 23 Jan 2003
Posts: 349
Location: Stockholm / Sweden

PostPosted: Wed Sep 17, 2003 2:56 pm    Post subject: Reply with quote

Ox- wrote:
Here, the getline function needs a buffer to put the string.

Actually not. The getline function allocates it's own buffer when the pointer is null. See "man getline" for details.
Back to top
View user's profile Send private message
Sleeper
l33t
l33t


Joined: 12 Nov 2002
Posts: 667

PostPosted: Wed Sep 17, 2003 3:02 pm    Post subject: Reply with quote

cederberg wrote:

Actually not. The getline function allocates it's own buffer when the pointer is null. See "man getline" for details.


Code:
No manual entry for getline
Back to top
View user's profile Send private message
Ox-
Guru
Guru


Joined: 19 Jun 2003
Posts: 305

PostPosted: Wed Sep 17, 2003 3:16 pm    Post subject: Reply with quote

cederberg wrote:
Ox- wrote:
Here, the getline function needs a buffer to put the string.

Actually not. The getline function allocates it's own buffer when the pointer is null. See "man getline" for details.

I did that before posting and have no man page for getline (old SuSE 7 system at work). Regardless, that is completely bizarre behaviour for a C function.

Oh well, sorry for having wasted everyone's time.
Back to top
View user's profile Send private message
cederberg
Guru
Guru


Joined: 23 Jan 2003
Posts: 349
Location: Stockholm / Sweden

PostPosted: Wed Sep 17, 2003 3:20 pm    Post subject: Reply with quote

Sleeper wrote:
No manual entry for getline

Try "emerge sys-apps/man-pages".

I think this package should have been installed on your system, as I cannot find any entry for it in my world file (and no relevant USE flag either)...
Back to top
View user's profile Send private message
cederberg
Guru
Guru


Joined: 23 Jan 2003
Posts: 349
Location: Stockholm / Sweden

PostPosted: Wed Sep 17, 2003 3:23 pm    Post subject: Reply with quote

Ox- wrote:
Regardless, that is completely bizarre behaviour for a C function.

Indeed. Everyone seems to recommend fgets these days, although noone seems to have the courage to issue a compiler warning for gets (and possibly getline).
Back to top
View user's profile Send private message
OdinsDream
Veteran
Veteran


Joined: 01 Jun 2002
Posts: 1057

PostPosted: Wed Sep 17, 2003 8:40 pm    Post subject: Reply with quote

cederberg wrote:
Ox- wrote:
Here, the getline function needs a buffer to put the string.

Actually not. The getline function allocates it's own buffer when the pointer is null. See "man getline" for details.


It sure does.

The line-reading section of my code is directly from man getline. I had enough trouble getting it to work with my own variables, so I resorted to cut-n-paste.

Your previous suggestion worked very well, cederberg. Thanks.
_________________
s/(?<!gnu\/)linux(?! kernel)/GNU\/Linux/gi

Don't blame me. I didn't vote for him.

http://john.simplykiwi.com
Back to top
View user's profile Send private message
OdinsDream
Veteran
Veteran


Joined: 01 Jun 2002
Posts: 1057

PostPosted: Wed Sep 17, 2003 8:40 pm    Post subject: Reply with quote

cederberg wrote:
Ox- wrote:
Here, the getline function needs a buffer to put the string.

Actually not. The getline function allocates it's own buffer when the pointer is null. See "man getline" for details.


It sure does.

The line-reading section of my code is directly from man getline. I had enough trouble getting it to work with my own variables, so I resorted to cut-n-paste.

Your previous suggestion worked very well, cederberg. Thanks.
_________________
s/(?<!gnu\/)linux(?! kernel)/GNU\/Linux/gi

Don't blame me. I didn't vote for him.

http://john.simplykiwi.com
Back to top
View user's profile Send private message
OdinsDream
Veteran
Veteran


Joined: 01 Jun 2002
Posts: 1057

PostPosted: Wed Sep 17, 2003 8:41 pm    Post subject: Reply with quote

cederberg wrote:
Ox- wrote:
Here, the getline function needs a buffer to put the string.

Actually not. The getline function allocates it's own buffer when the pointer is null. See "man getline" for details.


It sure does.

The line-reading section of my code is directly from man getline. I had enough trouble getting it to work with my own variables, so I resorted to cut-n-paste.

Your previous suggestion worked very well, cederberg. Thanks.
_________________
s/(?<!gnu\/)linux(?! kernel)/GNU\/Linux/gi

Don't blame me. I didn't vote for him.

http://john.simplykiwi.com
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum