Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

ublog's blog

By ublog, 13 years ago, In English

Why this code works? why it didn't get Runtime & compilation error? (runtime for a[-1] and compilation error for a[n]) maybe new c++ futures ??!!

int n;
int main () 
{
    cin>>n;
    int a[n];
    a[-1]=40;
    cout<<a[-1];
    return 0;
}
Tags c++
  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
13 years ago, hide # |
← Rev. 2  
Vote: I like it -8 Vote: I do not like it

С++ reserves that cell in memory

»
13 years ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

This question is very popular among beginner users of C++. Here is some answer. And here is a bunch of another.

»
13 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

And yes, int a[n] is valid code, it works like int *a = new int[n]. As I remember for C, it works in C99, not elder versions.