Hope this will helpful for someone.
Never repeat my mistake. Never start name of global variable with an underscore.
For example Accepted code in Codechef.
The same code gives Runtime error with addition:#define where _end
, i.e by renaming where variable to _end.
Again, the same code gives Wrong Answer with addition: #define size _end
, i.e by renaming another variable "size" to _end.
In my point of view, variables beginning with an underscore are reserved in the global namespaces.
Can somebody better explain what is happening here? I tried in some problems in Codeforces, but couldn't trigger it.
I guess it has something to do with this and that. In short: on Linux,
ld
(gcc
's linker) uses_end
symbol to mark end of all segments in the executable. Codeforces uses Windows, so it can be not the case here.My assumption: your variable named
_end
overrides this symbol and then strange things happen E.g. less memory is allocated , not everything is zeroed before execution, some memory is allocated twice: for a global variable and as part of heap.Try declaring this variable static (global variables marked 'static' does not export from the object file), like this:
static int where[5010];
. UPD: see andreyv's comment below, declaring global variables starting with underscore is undefined behavior, no matter what.Wow, this is the first time I heard of something like this.
Is there any other name that we must avoid? Should I declare all variables/methods as static? I see that bmerry always does this, maybe his reason was also related to this..
You should just avoid using reserved identifiers. From the C standard:
As you can see, Madiyar did just that — defined a reserved identifier in the program, and this resulted in undefined behavior. Making the variable
static
does not solve the problem, the behavior is still undefined.You should avoid all POSIX names if you submit solution on any GCC compiler. Such functions do not start with underscore.
I have seen how one good team got RE1 because they defined a global variable
read
. Inside main, they wrote something likeread = 1;
, and after that they read the input data usingcin
orscanf
(don't remember exactly). Deep inside those C/C++ calls theread
function was called (which was now a global variable), so it crashed.This is a problem only when linking runtime statically though.
P.S. The particular case was on MinGW GCC, and in the end the team resubmitted under MSVC which worked properly.