mercredi 3 juin 2015

Why can I set values outside of the array's range?

From what I have read, arrays can only be a fixed size, which is defined when you create it (and if you want to create something similar with a dynamic size, you use vectors instead).

However, when I try to set values outside of the array, I am perfectly able to:

int badNums[5] = {4, 13, 14, 24, 34};
badNums[999] = -127;
std::cout << badNums[999] << std::endl;
// Returns -127

And it gets even more bizarre if I try to access values of the array outside of the fixed range:

std::cout << badNums[997] << std::endl;
// Returns 825245046 (but seems to be randomly chosen every time I run it)

What is going on here?

Aucun commentaire:

Enregistrer un commentaire