i’m fairly new to programming and still have no idea why its happening or how to fix this exception im getting when running this program i’m making…
How do exceptions occur anyway? Well here’s the 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()
{
for (int I = 0; I <56; I++)
{
cout << _contents[I];
}
};
/////////////////////////////////////////// Insert ///////////////////////
bool Insert(vector <string> _string)
{
vector<string>::const_iterator I;
int y = 0;
for (I = _string.begin(); I != _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;
for (vector <string>::const_iterator I = Map.begin(); I != Map.end(); I++)
{
Map[offset] = _lines_[offset];
offset++;
}
Screen theScreen;
theScreen.Insert(Map);
theScreen.Display();
char response;
cin >> response;
return 0;
}
I’m getting this exception:
First-chance exception at 0x5acfc9c7 (msvcr100d.dll) in TestGame.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Unhandled exception at 0x5acfc9c7 (msvcr100d.dll) in TestGame.exe: 0xC0000005: Access violation reading location 0xcccccccc.
pointing to this line of code in “memcpy.asm”:
185 rep movsd ;N - move all of our dwords
Thanks!!
You create a
vectorwith 56 elements in it:Then you define an array containing five
stringobjects in it:Then you try to read 56
stringobjects from that array:Since there are only five elements in the array, you are reading uninitialized memory (or memory that is initialized but probably doesn’t contain
stringobjects) and treating that memory as if it containsstringobjects.I’m not quite sure what you’re trying to do, but why not just insert the strings directly into the
vector?Or use the
std::copyalgorithm: