C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 8th, 2012, 11:27 AM
elenizi elenizi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 elenizi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #2  
Old October 8th, 2012, 12:20 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
All your strtok() calls except the first, should have NULL for their first argument.

Reply With Quote
  #3  
Old October 8th, 2012, 12:49 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
Also, at each step, you should have
if ( result != NULL )
before passing result onto atoi()
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #4  
Old October 8th, 2012, 01:08 PM
elenizi elenizi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 elenizi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 42 sec
Reputation Power: 0
tried your advice but the same error appears =/

Reply With Quote
  #5  
Old October 8th, 2012, 01:21 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 33 m 51 sec
Reputation Power: 383
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!

Reply With Quote
  #6  
Old October 8th, 2012, 02:00 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Segmentation fault problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap