Segmentation Fault

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 24
Reputation: TrintiyNoe is an unknown quantity at this point 
Solved Threads: 0
TrintiyNoe TrintiyNoe is offline Offline
Newbie Poster

Segmentation Fault

 
0
  #1
Jul 5th, 2009
  1. #include<iostream>
  2. #include<vector>
  3. #include<sstream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<string>
  7. using namespace std;
  8.  
  9. class ComboLength
  10. {
  11. public:
  12. int howLong(string moves)
  13. {
  14. vector<int> cc;
  15. int i,j,k=0;
  16. for(i=0;i<moves.size()-1;++i)
  17. {
  18. if(moves[i]==moves[i+1]) //Error
  19. k++;
  20. else
  21. cc.push_back(k);
  22. }
  23. return *max_element(cc.begin(),cc.end());
  24. }
  25. };
I get a segementaion error in line if (moves[i]==moves[i+1])
any idea why?
Last edited by TrintiyNoe; Jul 5th, 2009 at 3:31 am. Reason: Added code tages
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Segmentation Fault

 
0
  #2
Jul 5th, 2009
Why calling the function moves.size() again and again for every loop ??? Do it once,else its just a waste of computation time.

Do this :
  1. int max=moves.size()-1;
  2. for(i=0;i<max;++i)
  3. {
  4. if(moves[i]==moves[i+1])
  5. k++;
  6. else
  7. cc.push_back(k);
  8. }

And with respect to the segmentation fault I am not finding any problem with the code.Well may be its not because of the statement you said.
Last edited by csurfer; Jul 5th, 2009 at 4:06 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Segmentation Fault

 
0
  #3
Jul 5th, 2009
Originally Posted by TrintiyNoe View Post
  1. #include<iostream>
  2. #include<vector>
  3. #include<sstream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<string>
  7. using namespace std;
  8.  
  9. class ComboLength
  10. {
  11. public:
  12. int howLong(string moves)
  13. {
  14. vector<int> cc;
  15. int i,j,k=0;
  16. for(i=0;i<moves.size()-1;++i)
  17. {
  18. if(moves[i]==moves[i+1]) //Error
  19. k++;
  20. else
  21. cc.push_back(k);
  22. }
  23. return *max_element(cc.begin(),cc.end());
  24. }
  25. };
I get a segementaion error in line if (moves[i]==moves[i+1])
any idea why?
i can see the problem here..

  1. return *max_element(cc.begin(),cc.end());

it shouldn't be like that, what if no values are being inserted in the vector. and your test [icode] moves[i] == moves[i+1] succeeded all the time then vector "cc" will be empty and max_elements return the last iterator which is not an element. and dereferencing an unknown element is segmentation fault.. (in complete description but sufficient at this level).

so change this code to following.
  1. vector<int>::iterator maxIter = max_element(cc.begin(),cc.end());
  2. if (maxIter != cc.end()){
  3. return *maxIter; // not a good way either. but will work.
  4. }
  5. else {
  6. return -1; // logically indicatting no value depends on implementation.
  7. }

Hope this helps
Last edited by Laiq Ahmed; Jul 5th, 2009 at 5:43 am. Reason: added few lines
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC