943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 350
  • C++ RSS
Apr 26th, 2009
0

strcmp seg faulting in a loop

Expand Post »
Hi,
Here's my code
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Apr 26th, 2009
0

Re: strcmp seg faulting in a loop

> The function cIndex isn't always returning a value:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 26th, 2009
0

Re: strcmp seg faulting in a loop

To release the allocated memory (of your featlist ):
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 26th, 2009
0

Re: strcmp seg faulting in a loop

ok...It was the return value! thanks a lot! memory leaf fixed
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reading a mathematical expression from a file
Next Thread in C++ Forum Timeline: problem c++ (My Pi)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC