Like Tree1Likes
  • 1 Post By Salem

debuging problem

This is a discussion on debuging problem within the C++ Programming forums, part of the General Programming Boards category; hey everyone, I have written a code, which compiles but when Im trying to debug it I get a window ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    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);   
        }
    Last edited by Salem; 04-07-2011 at 05:48 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > "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.
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21