5

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.

7
  • @AlterMann Well that won't explain a segfault for me obviously? Commented May 1, 2015 at 16:22
  • 1
    works in my environment ... maybe ideone.com sucks? Commented May 1, 2015 at 16:22
  • 1
    Maybe it differs by compilers? I will add the compiler information. Commented May 1, 2015 at 16:30
  • 1
    Looks like _end is defined as something else on your platform (it's a reserved name, after all). Commented May 1, 2015 at 16:32
  • 2
    I've noticed it works well, as soon you place int _end[1111] {0}; inside of main's body: ideone.com/zT9ZII It's strange behavior I agree. Seems to have something to do with initialization of _end. Also using prefix _ for symbols might call strange side effects, as these are actually reserved for implementation. Commented May 1, 2015 at 16:35

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

With GCC in particular, _end is a pointer to the end of the program. Changing the name of the variable makes the program work.
See also this answer which discusses C++'s rules around reserved identifiers: stackoverflow.com/a/228797

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.