So I'm working with a dynamically allocated array, and I've set it to hold 5 elements, which should thus be 0-4. I made a function to reserve it if necessary, but I wanted to see if I was getting the program crash I expected when I assigned a value to the array at [5]. But, no error, not until [6].
Here's my code:
int* dynamic_arr;
dynamic_arr = new int[5];
for(int i = 0; i <= 100; i++){
dynamic_arr[i] = i;
used++;
cout << dynamic_arr[i]<<endl;
}
Here's the output:
0 //i[0]
1 //i[1]
2 //i[2]
3 //i[3]
4 //i[4]
5 //i[5] <-- should be out of range
After that it crashes.
Why am I able to assign a value to a portion of the array that is, well, out of range for lack of a better term?
Thanks
OpSrcFTW
EDIT: Appreciate the answers guys, thanks. I'll read more before posting for a quick answer next time, sorry.