| | |
Segmentation Fault
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 24
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<vector> #include<sstream> #include<algorithm> #include<cmath> #include<string> using namespace std; class ComboLength { public: int howLong(string moves) { vector<int> cc; int i,j,k=0; for(i=0;i<moves.size()-1;++i) { if(moves[i]==moves[i+1]) //Error k++; else cc.push_back(k); } return *max_element(cc.begin(),cc.end()); } };
any idea why?
Last edited by TrintiyNoe; Jul 5th, 2009 at 3:31 am. Reason: Added code tages
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 :
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.
Do this :
c++ Syntax (Toggle Plain Text)
int max=moves.size()-1; for(i=0;i<max;++i) { if(moves[i]==moves[i+1]) k++; else cc.push_back(k); }
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"....
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
•
•
•
•
I get a segementaion error in line if (moves[i]==moves[i+1])C++ Syntax (Toggle Plain Text)
#include<iostream> #include<vector> #include<sstream> #include<algorithm> #include<cmath> #include<string> using namespace std; class ComboLength { public: int howLong(string moves) { vector<int> cc; int i,j,k=0; for(i=0;i<moves.size()-1;++i) { if(moves[i]==moves[i+1]) //Error k++; else cc.push_back(k); } return *max_element(cc.begin(),cc.end()); } };
any idea why?
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
vector<int>::iterator maxIter = max_element(cc.begin(),cc.end()); if (maxIter != cc.end()){ return *maxIter; // not a good way either. but will work. } else { return -1; // logically indicatting no value depends on implementation. }
Hope this helps
Last edited by Laiq Ahmed; Jul 5th, 2009 at 5:43 am. Reason: added few lines
![]() |
Similar Threads
- segmentation fault (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unix/C++ segmentation fault (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: substitution cipher help
- Next Thread: input length
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





