G
gbisht
Hi,
I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.
Thanks,
Gautam.
// main.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <memory>
using namespace std;
int main(){
int *x = new int [10];
for ( int i =0; i<10; i++)
x = i;
for ( int i =0; i<20; i++)
cout << x<< endl;;
return 0;
}
////////////////////////////
$ g++ main.cpp
$ ./a.out
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.
Thanks,
Gautam.
// main.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <memory>
using namespace std;
int main(){
int *x = new int [10];
for ( int i =0; i<10; i++)
x = i;
for ( int i =0; i<20; i++)
cout << x<< endl;;
return 0;
}
////////////////////////////
$ g++ main.cpp
$ ./a.out
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0