By Rishab Narang
Code snippet follows.
#include <iostream>
#include <vector>
using namespace std;
const int SIZE = 2;
int main()
{
int rawarray[SIZE];
vector <int> v(SIZE);
int i;
for (i = 0; i <= SIZE+1; i++)
{
rawarray[i] = i;
v[i] = i;
}
for (i = 0; i <= SIZE+1; i++)
{
cout << "rawarray[" << i << "] = " << rawarray[i] << endl;
cout << "v[" << i << "] = " << v[i] << endl;
}
return 0;
}
When I compile and run this program, there is no compilation as well as runtime error. However, it is clear from the code given above that each of the rawarray and the STL vector object v is supposed to contain two elements as per specified size i.e. 2 but when I try to put something as the third or even fourth element, they are being allowed without any warning or error. Here’s the output:
rawarray[0] = 0
v[0] = 0
rawarray[1] = 1
v[1] = 1
rawarray[2] = 2
v[2] = 2
rawarray[3] = 3
v[3] = 3
Any suggestions or workaround ?
#include <iostream>
#include <vector>
using namespace std;
const int SIZE = 2;
int main()
{
int rawarray[SIZE];
vector <int> v(SIZE);
int i;
for (i = 0; i <= SIZE+1; i++)
{
rawarray[i] = i;
v[i] = i;
}
for (i = 0; i <= SIZE+1; i++)
{
cout << "rawarray[" << i << "] = " << rawarray[i] << endl;
cout << "v[" << i << "] = " << v[i] << endl;
}
return 0;
}
When I compile and run this program, there is no compilation as well as runtime error. However, it is clear from the code given above that each of the rawarray and the STL vector object v is supposed to contain two elements as per specified size i.e. 2 but when I try to put something as the third or even fourth element, they are being allowed without any warning or error. Here’s the output:
rawarray[0] = 0
v[0] = 0
rawarray[1] = 1
v[1] = 1
rawarray[2] = 2
v[2] = 2
rawarray[3] = 3
v[3] = 3
Any suggestions or workaround ?