strcmp seg faulting in a loop

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

strcmp seg faulting in a loop

 
0
  #1
Apr 26th, 2009
Hi,
Here's my code
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string.h>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. #include<stdio.h>
  7.  
  8. using namespace std;
  9.  
  10. int cIndex(char *x,char **featlist,int len)
  11. {
  12. int i;
  13. for(i=0;i<len;i++)
  14. if(strcmp(x,featlist[i])==0)
  15. return i;
  16. }
  17.  
  18. int main(int argc,char *argv[])
  19. {
  20. std::ifstream featFile(argv[1],ios::in);
  21. std::ifstream trainFile(argv[2],ios::in);
  22. // std::ofstream svmFile(argv[3],ios::out);
  23. char line[20000];
  24. char feat[100];
  25. char **featlist;
  26. int i=0,len=0,fl=0,j;
  27. char* x;
  28. int valnum;
  29. while(featFile.good())
  30. {
  31. featFile.getline(feat,100,'\n');
  32. if(featFile.eof()) break;
  33. len++;
  34. }
  35. featFile.clear();
  36. featFile.seekg(0,ios::beg);
  37. // cout<<len<<endl;
  38. featlist=new char*[len];
  39. for (i = 0; i < len; ++i)
  40. featlist[i] = new char[100];
  41. i=0;
  42. while(featFile.good())
  43. {
  44. featFile.getline(feat,100,'\n');
  45. if(featFile.eof()) break;
  46. strcpy(featlist[i],feat);
  47. i++;
  48. }
  49. while(trainFile.good())
  50. {
  51. trainFile.getline(line,20000,'\n');
  52. x=strtok(line," ");
  53. valnum=cIndex(x,featlist,len);
  54. cout<<valnum<<";";
  55.  
  56. }
  57. trainFile.close();
  58.  
  59. }

my featFile is of the form
  1. yasuhiro
  2. ali
  3. mdbl
  4. consum
  5. dollar
  6. unrest
  7. protest
  8. mile
  9. mill
  10. ventur
  11. semiconductor
  12. heller
  13. authoris

and my trainFIle is of the form
  1. acq 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 26:0 27:0 28:0 29:0 30:0 31:0 32:0 33:0 34:0
  2. acq 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 26:0 27:0 28:0 29:0 30:0 31:0 32:0 33:0 34:0

so i basically have to read the training file and replace the first name with an array index...the array index is formed using the featFile...the call to cIndex keeps seg faulting after some iterations.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: strcmp seg faulting in a loop

 
0
  #2
Apr 26th, 2009
> The function cIndex isn't always returning a value:
  1. int cIndex(char *x,char **featlist,int len)
  2. {
  3. int i;
  4. for(i=0;i<len;i++)
  5. if(strcmp(x,featlist[i])==0)
  6. return i;
  7. /* So what happens when featlist[i] and x aren't equal ? */
  8. /* You should add a return code to check whether they were equal or not */
  9. /* Let's say if they weren't equal you return -1 for example */
  10. }

> Your code compiled and ran fine for me (what OS and compiler are you using?)

> Running your code gave me the following output: -7;-7; , is that correct? (I'm using the same file contents for featFile and trainFile as you provided)

> I can't seem to find any place in your code where you're deallocating the assigned memory to featlist , that's a serious memory leak ...
Last edited by tux4life; Apr 26th, 2009 at 6:06 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: strcmp seg faulting in a loop

 
0
  #3
Apr 26th, 2009
To release the allocated memory (of your featlist ):
  1. for(int i = 0; i < len; i++)
  2. delete[] ptr[i];
So, no more memory leaks
Last edited by tux4life; Apr 26th, 2009 at 6:20 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: strcmp seg faulting in a loop

 
0
  #4
Apr 26th, 2009
ok...It was the return value! thanks a lot! memory leaf fixed
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC