How does this work?

I was testing something randomly and plugged this in and it works. I don't know why because nothing I've read suggested it would.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int * pointer;

int main(){

pointer = new int;

pointer[0] = 3;
pointer[1] = 4;
pointer[2] = 6;
pointer[3] = 7;
pointer[4] = 9;
pointer[20] = 165;
pointer[80] = 2;
pointer[10000] = 2;

for ( int i = 0; i < 10000; i++ )
{
	cout << pointer[i] << " ";
}

return 0;

}


Everything ran and all the information printed. I've also tried putting it with a size of 3 or something small and it still works.
Last edited on
since the pointed memory is in heap or memory pool, as long as the block of the memory that the pointed memory resides is mapped into the process, it's accessible.
So I could just add elements to dynamic arrays just willy nilly?
You can access the array doesn't mean you are safe to access it. Because the system or the tool of memory pool is officially managing the piece of memory, they might write something after the address of "pointer + sizeof(int)" and break your array.
Okay, I'm just curious is there any way to read any spot on the memory.
Topic archived. No new replies allowed.