LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-18-2004, 05:31 PM   #1
linorg
Member
 
Registered: Nov 2003
Location: delhi
Distribution: redhat 8.0 ,fedora
Posts: 32

Rep: Reputation: 15
segmentation fault in following peice of code :please help


hi
segmentation fault in foll code is driving me nuts..
i am basically reading an ascii file in which nums are stored seperated by commas
i manipulate these numbers and store in another ascii file
please help ,this code is vital to working of my project to be submiited by monday 23rd march


when i ran gdb it told that the foll code caused the segmentation fault

number+=(num[lnum]*pow(10,lnum))



the whole prg is listed below
code..


#include<stdio.h>
#include<string.h>
#include<math.h>
int main(){
FILE*ascii;
FILE *ascii2;
double data;
char c;
int i=0,flag=0,numcomma=0,lnum,sign,j=0;
double number=0;
char num[5];
ascii=fopen("/home/shobhit/das/tempo-1.1.4/fft/comma.ascii","r");
ascii2=fopen("/home/shobhit/das/tempo-1.1.4/fft/newline4.txt","w");
while (!feof(ascii))
{ j++;
if(j>5)
break;
if(c==','&&numcomma==2){
lnum=i;i=0;flag=1;//beginning of new num
}


if(lnum&&!i)
{number=0;
if(num[0]=='-')
{sign=-1;lnum-=2;}
else {sign=1;lnum=lnum-1;}
for(;lnum>=0;lnum--){

number+=(num[lnum]*pow(10,lnum));}//this line gives the segmetation fault

if(sign==-1)
number*=-1;
data=(number+2048)*0.09645-199;
fprintf(ascii2,"%lf ",data);
}

c=getc(ascii);
if(flag==1)
{ if(c==',')break;//for 2nd comma ie ,123,

num[i]=c;
i++;}
if(c==',')
{ flag=1;numcomma++;}

}



fclose(ascii);
fclose(ascii2);
return 0;
}


thanks
linorg
 
Old 03-18-2004, 08:28 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
plz use code tags to post code! it seems that perhaps lnum is never being initialized or set to a value,
Code:
while (!feof(ascii))
{ j++;
if(j>5)
break;
if(c==','&&numcomma==2){
lnum=i;i=0;flag=1;//beginning of new num
}
im guessin that if statement never comes true, and lnum is never set to a value. so it has some stack garbage in it. try initializng lnum to 0 and see if that helps.
 
Old 03-18-2004, 08:57 PM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417

Rep: Reputation: 31
Simple explanation:
When the program reaches number+=(num[lnum]*pow(10,lnum));
LNUM can be greater than num[]'s capacity, so it causes a segfault.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
fsck fails. Code: Bad EIP value. Segmentation fault. MOHCTP Debian 4 09-07-2005 08:35 PM
dpkg returned an error code (1) Segmentation fault questionman Debian 6 11-02-2004 12:04 AM
segmentation fault in c++ drigz Programming 10 06-16-2004 03:06 PM
Segmentation fault (Help) neeraj_bod General 1 04-11-2002 09:46 AM


All times are GMT -5. The time now is 10:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration