c++ - Exception Problems using the STL string and vector class -


i'm new programming , still have no idea why happening or how fix exception im getting when running program i'm making... how exceptions occur anyway? here's code:

#include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std;  /////////////////////////   screen class    //////////////////////////// class screen { private: ///////////////////////////////////////////  screen variables //////////////     string _name;     string _contents[56];  public:     screen(){};     ~screen(){}; //////////////////////////////////////////// display    ///////////////     void display()     {          (int = 0; <56; i++)         {             cout << _contents[i];         }     }; ///////////////////////////////////////////  insert ///////////////////////     bool insert(vector <string> _string)     {         vector<string>::const_iterator i;         int y = 0;         (i = _string.begin(); != _string.end(); i++)         {             _contents[y] = _string[y];             y++;         }         return true;     };   };  /////////////////////////////////////////////  main //////////////////////// int main() {     vector <string> map(56);      string _lines_[] = {"hi", "holla", "eyo", "whatsup", "hello"};      int offset = 0;     (vector <string>::const_iterator = map.begin(); != map.end(); i++)     {         map[offset] = _lines_[offset];         offset++;     }      screen thescreen;     thescreen.insert(map);     thescreen.display();      char response;     cin >> response;     return 0;  } 

i'm getting exception:

first-chance exception @ 0x5acfc9c7 (msvcr100d.dll) in testgame.exe: 0xc0000005: access violation reading location 0xcccccccc. unhandled exception @ 0x5acfc9c7 (msvcr100d.dll) in testgame.exe: 0xc0000005: access violation reading location 0xcccccccc. 

pointing line of code in "memcpy.asm":

185        rep     movsd           ;n - move of our dwords 

thanks!!

you create vector 56 elements in it:

vector <string> map(56);  

then define array containing 5 string objects in it:

string _lines_[] = {"hi", "holla", "eyo", "whatsup", "hello"}; 

then try read 56 string objects array:

                                     v 56 elements between begin() , end() (vector <string>::const_iterator = map.begin(); != map.end(); i++) {     map[offset] = _lines_[offset];                   ^ reading i'th element of array 

since there 5 elements in array, reading uninitialized memory (or memory initialized doesn't contain string objects) , treating memory if contains string objects.

i'm not quite sure you're trying do, why not insert strings directly vector?

vector<string> map; map.push_back("hi"); map.push_back("holla"); // etc. 

or use std::copy algorithm:

int elements_in_lines = sizeof(_lines_) / sizeof(_lines_[0]); std::copy(_lines_, _lines_ + elements_in_lines, std::back_inserter(map)); 

Comments