c++ - Does visual studio let you access outside the bound of arrays? (VS2013) -


this question has answer here:

i'd formal explanation on stance of visual studio c++ , when create array such as:

int a[3] = {1, 2, 3}; 

and like:

cout << a[4]; 

upon test, print out garbage stored in memory location. why allow programmer this, while language javascript prevent user doing so?

what's prevailing philosophy not making compiler ban kind of behavior user in c++? carried on c?

these minor curiosities have, , perhaps person answers can tell me i'd able find such information. answer what happening not i'm asking, it's why i'm interested in.

thank you.

it has nothing compiler, language defined in such way it's allowed. lead undefined behavior though contents indeterminate.

as reason it's allowed, consider definition of subscript (array indexing) operator (from iso/iec 14882:2011(e) §5.2.1/1 [expr.sub]):

a postfix expression followed expression in square brackets postfix expression. 1 of expressions shall have type “pointer t” , other shall have unscoped enumeration or integral type. result lvalue of type “t.” type “t” shall completely-defined object type. expression e1[e2] identical (by definition) *((e1)+(e2))

[emphasis mine]

what happens due above array e1 decayed pointer first element (i.e. it's equivalent &(e1)[0]), , compiler performs pointer arithmetic (e1)+(e2). , since array has decayed pointer, there no possibility bounds-checking.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -