View previous topic :: View next topic |
Author |
Message |
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Tue Sep 16, 2003 2:00 pm Post subject: Good C resources? Reading line-by-line segfault... |
|
|
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 |
|
|
cederberg Guru
Joined: 23 Jan 2003 Posts: 349 Location: Stockholm / Sweden
|
Posted: Tue Sep 16, 2003 2:46 pm Post subject: Re: Good C resources? Reading line-by-line segfault... |
|
|
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 |
|
|
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Tue Sep 16, 2003 4:17 pm Post subject: |
|
|
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... _________________ s/(?<!gnu\/)linux(?! kernel)/GNU\/Linux/gi
Don't blame me. I didn't vote for him.
http://john.simplykiwi.com |
|
Back to top |
|
|
cederberg Guru
Joined: 23 Jan 2003 Posts: 349 Location: Stockholm / Sweden
|
Posted: Tue Sep 16, 2003 4:36 pm Post subject: |
|
|
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 |
|
|
ed0n l33t
Joined: 23 Apr 2003 Posts: 638 Location: Prishtine/Kosove
|
Posted: Tue Sep 16, 2003 4:46 pm Post subject: |
|
|
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 |
|
|
cederberg Guru
Joined: 23 Jan 2003 Posts: 349 Location: Stockholm / Sweden
|
Posted: Tue Sep 16, 2003 5:02 pm Post subject: |
|
|
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 |
|
|
ed0n l33t
Joined: 23 Apr 2003 Posts: 638 Location: Prishtine/Kosove
|
Posted: Wed Sep 17, 2003 11:18 am Post subject: |
|
|
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 |
|
|
Ox- Guru
Joined: 19 Jun 2003 Posts: 305
|
Posted: Wed Sep 17, 2003 2:42 pm Post subject: |
|
|
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: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 |
|
|
cederberg Guru
Joined: 23 Jan 2003 Posts: 349 Location: Stockholm / Sweden
|
Posted: Wed Sep 17, 2003 2:56 pm Post subject: |
|
|
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 |
|
|
Sleeper l33t
Joined: 12 Nov 2002 Posts: 667
|
Posted: Wed Sep 17, 2003 3:02 pm Post subject: |
|
|
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 |
|
|
Ox- Guru
Joined: 19 Jun 2003 Posts: 305
|
Posted: Wed Sep 17, 2003 3:16 pm Post subject: |
|
|
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 |
|
|
cederberg Guru
Joined: 23 Jan 2003 Posts: 349 Location: Stockholm / Sweden
|
Posted: Wed Sep 17, 2003 3:20 pm Post subject: |
|
|
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 |
|
|
cederberg Guru
Joined: 23 Jan 2003 Posts: 349 Location: Stockholm / Sweden
|
Posted: Wed Sep 17, 2003 3:23 pm Post subject: |
|
|
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 |
|
|
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Wed Sep 17, 2003 8:40 pm Post subject: |
|
|
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 |
|
|
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Wed Sep 17, 2003 8:40 pm Post subject: |
|
|
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 |
|
|
OdinsDream Veteran
Joined: 01 Jun 2002 Posts: 1057
|
Posted: Wed Sep 17, 2003 8:41 pm Post subject: |
|
|
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 |
|
|
|