Madiyar's blog

By Madiyar, 9 years ago, In English

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.

Tags c++
  • Vote: I like it
  • +43
  • Vote: I do not like it

»
9 years ago, # |
Rev. 3   Vote: I like it +33 Vote: I do not like 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.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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..

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +33 Vote: I do not like it

      You should just avoid using reserved identifiers. From the C standard:

      — All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
      — All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
      . . .
      If the program declares or defines an identifier in a context in which it is reserved, or defines a reserved identifier as a macro name, the behavior is undefined.

      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.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 4   Vote: I like it +16 Vote: I do not like it

      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 like read = 1;, and after that they read the input data using cin or scanf (don't remember exactly). Deep inside those C/C++ calls the read 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.