The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Segmentation fault problem
Discuss Segmentation fault problem in the C Programming forum on Dev Shed. Segmentation fault problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 8th, 2012, 11:27 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4
Time spent in forums: 17 m 42 sec
Reputation Power: 0
|
|
|
Segmentation fault problem
hi i want to make a parser that reads from a file and saves thetokens into a struct i am using this code but i keep getting segmentation fault
Code:
#include <stdio.h>
#include <string.h>
#include <strings.h>
typedef struct{
int label;
int id;
int r1;
int r2;
int r3;
}instruction;
int main(int argc,char *argv[])
{
FILE *input;
char str[1000];
char delim[] = " ";
char *result;
input = fopen(argv[1], "r"); // error check this!
int i=0;
instruction *instr = (instruction*)(malloc(100*sizeof(instruction)));
while (fgets(str,1000,input)!=NULL){
result = strtok(str, delim);
instr[i].label = atoi(result);
result = strtok(NULL, delim);
instr[i].id = atoi(result);
result = strtok(str, delim);
instr[i].r1 = atoi(result);
result = strtok(NULL, delim);
instr[i].r2 = atoi(result);
result = strtok(str,delim);
instr[i].r3 = atoi(result);
i++;
}
fclose(input);
return 0;
}
|

October 8th, 2012, 12:20 PM
|
|
|
All your strtok() calls except the first, should have NULL for their first argument.
|

October 8th, 2012, 12:49 PM
|
 |
Contributed User
|
|
|
|
|
Also, at each step, you should have
if ( result != NULL )
before passing result onto atoi()
|

October 8th, 2012, 01:08 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4
Time spent in forums: 17 m 42 sec
Reputation Power: 0
|
|
|
tried your advice but the same error appears =/
|

October 8th, 2012, 01:21 PM
|
 |
Contributing User
|
|
|
|
By chance when you invoke your program do you pass it a file name argument? If you don't your argv[1] is NULL.
Code:
$ supreme_parser filename.goes.here.my_funky_language
__________________
[code] Code tags[/code] are essential for python code!
|

October 8th, 2012, 02:00 PM
|
 |
Contributed User
|
|
|
|
|
Don't just post "I tried and it didn't work". You need to post actual code changes and actual test commands and results.
> input = fopen(argv[1], "r"); // error check this!
So DO IT!
- check argv[1] actually exists as a command line argument.
- check that the file was opened successfully.
> instruction *instr = (instruction*)(malloc(100*sizeof(instruction)));
CHECK IT!
While you're at it
- remove the cast, so it's instruction *instr = malloc(100*sizeof(instruction));
- add #include <stdlib.h>
If you get some error message about "cannot convert void*", then change your filename / build process so it compiles C code and not C++ code.
Casting malloc in C is as best a waste of time, and at worst a dangerous bug.
At it's most dangerous, the cast masks your failure to include stdlib.h (as you have done), at which point the compiler assumes extern int malloc();
But this is a lie (it returns a pointer). On machines where pointers are larger than ints (which is getting fairly common at the moment with 64-bit machines becoming widely available), the implicit int and cast will lose information, giving you a garbage pointer for your trouble (and handing you a segfault for your reward).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|