this is what i came up with:
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void ReadTheTest()
{
string myText;
string first;
int counter = 0;
ifstream MyReadFile("test.txt");
while (getline(MyReadFile, myText))
{
if (counter == 0)
{
first = myText;
}
else
{
if (first != myText)
{
cout << "bad address man.\n";
}
}
counter++;
}
MyReadFile.close();
}
void test(vector <int*> v)
{
for (int i = 0; i < v.size(); i++)
{
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v[i] << "\n";
myfile.close();
}
}
int main()
{
int j = 2;
vector <int*> v; // size of the vector starts at 0
for (int i = 0; i < 2; i++)
{
if (i != 1)
{
v.push_back(&i); // v now has 1 element
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v[i] << "\n";
myfile.close();
}
else
{
v.push_back(&j); // v now has 1 element
test(v);
v[1] = v[0];
}
test(v);
}
ReadTheTest();
}
in main, in the else, line 65, the element 0 wrote over element 1, which hides the fact that in line 63 the value j wrote to the element 1 too.
so for security when element 1 is going to be used it runs the test first. then if the vector element is wrote over its able to be identified.