By Madiyar, 5 years ago, ,

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.

• +43

 » 5 years ago, # | ← Rev. 3 →   +33 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.
•  » » 5 years ago, # ^ |   0 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..
•  » » » 5 years ago, # ^ |   +33 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.