Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array defined as "_end[LEN]" causes segmentation fault in C/C++ [duplicate]

I tried to define a global array, named _end, of size ~1000 in C/C++, but I got a segmentation fault even when I simply iterated it. Is the name "_end" very special in C/C++ that causes such problem? Or this can be a very serious bug... (The code is attached below, and it breaks in g++ 4.3.2, 4.5.2, 4.9.2, etc.)

#include <iostream>
using namespace std;

int _end[1111];

int main() {
    for (int i=0; i<1111; i++) {
        cout << i << endl;
        _end[i]++;
    }
    return 0;
}

You can see the result at https://ideone.com/XAcUeZ. See here also for the C compiler.

like image 836
Blue Bear Avatar asked May 01 '15 16:05

Blue Bear


1 Answers

Names which start with an underscore (or two) are reserved for the compiler. This is official C++ standard. Use at own risk.

like image 129
Greenflow Avatar answered Sep 28 '22 03:09

Greenflow