-
debuging problem
hey everyone,
I have written a code, which compiles but when Im trying to debug it I get a window that says:
"An access violation (segmentation fault) raised in your program".
Well, I also know that I have a infinite loop somewhere but i guess its not the real reason for that note.
Any help is appreciated.
Thanks.
The code(the problematic part):
Code:
void SplitMessage( const string& message, vector<string>& words, vector<string>& keys, string& final_key )
{
string::const_iterator main_iter=message.begin(), iter1 =message.begin(), iter2=message.begin();
size_t index1= 0;
size_t index2= 0;
string word, key;
for (; main_iter<message.end(); main_iter++)
{
while (*iter2 !=' ' && *iter2!='\0')
{
main_iter++;
iter2++;
}
index1= iter1-message.begin();
index2= iter2-iter1;
word=message.substr(index1,index2);
words.push_back(word);
iter2++;
iter1=iter2;
while (((*iter2) !=' ')&& *iter2!='\0')
{
main_iter++;
iter2++;
}
index1= iter1-message.begin();
index2= iter2-iter1-1;
key=message.substr(index1,index2);
keys.push_back(key);
iter2++;
iter1=iter2;
}
final_key= *((words.end())-1);
}
int main()
{
string message;
string const &re_message=message;
vector<string> key;
vector<string> &keys=key;
vector<string> word;
vector<string> &words=word;
string final;
string &final_key=final;
vector<string>::iterator iter_keys=keys.begin();
vector<string>::iterator iter_words=words.begin();
cout << "enter your message:" << std::endl;
getline (std::cin, message);
while (message != end_input)
{
SplitMessage(re_message,word,key,final_key );
/* DecodeMessage(words,keys);
PrintMessage(words,final_key);
cout.flush();
*/
cout << (*iter_keys)+" "+ (*iter_words) << std::endl;
keys.clear();
words.clear();
message.clear();
final.clear();
getline (cin, message);
}
-
-
> "An access violation (segmentation fault) raised in your program".
So run it in the debugger, and find out which memory reference is bad.
Then work out why you tried to access illegal memory, and fix the problem.
Knowing how to use the debugger is just as important as being able to edit code, compile and RTM.
-
Figuring out a segfault just by looking at code is extremely difficult. If you attach your full source code (as a file attachment if it's large) so it can actually be tested, I may be able to help.