This question already has answers here:
What are the rules about using an underscore in a C++ identifier?
(5 answers)
Closed 7 years ago.
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.
Names which start with an underscore (or two) are reserved for the compiler. This is official C++ standard. Use at own risk.
Related
This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Consistency of undefined behavior for a fixed compiler
(2 answers)
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Closed 19 days ago.
I use Dev C++ with "TDM-GCC 4.9.2 64-bit Debug"
and
compiler options is "-std=c++14"
When I run code below
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int a,b,c,d;
cout << a << endl;
cout << b << endl;
}
output
0
1
and use debugging,
why int a, b is 0 and 1??
If I do the same thing using Visual Studio, it won't be compiled because it uses uninitialized local variables.
but Dev C++ can compile.
This question already has answers here:
What happens to uninitialized variables? C++ [duplicate]
(3 answers)
Uninitialized variable behaviour in C++
(4 answers)
Closed 3 years ago.
I have some difficulty understanding what is happening with this simple example compiled by two different compilers:
#include <iostream>
using namespace std;
int main()
{
double x;
cout << x << endl;
x += 1.0;
cout << x << endl;
return 0;
}
Compiling with g++ it gives
0
1
while compiling with clang++, it gives
6.95279e-310
1
I am sure it is my lack of understanding, but any tips understanding this behaviour would be much appreciated. Is the compiled code produced from clang++ setting x = 0 when it is reassigned, but not when shown with cout? Is this expected behaviour?
Question: Should one always initialise variables because of these differences? What is common practice in C++?
What you are experiencing is undefined behaviour. In C++ variables are not initialized for you.
The output of your program might be different from compiler to compiler, it might even change it's outcome if you run it multiple times or on a different machine.
Visual Studio 2019 with SDL checks enabled won't even compile this.
So yes you should always initialise variables before using them.
This question already has answers here:
static const vs #define
(11 answers)
Closed 4 years ago.
Goal
In the end, I want to know if using #define is bad for your code, and why.
Code
#include <iostream>
using namespace std;
#define favouriteNumber 20;
int main()
{
int number = favouriteNumber;
cout << number;
}
According to Stroustrup it's particularly "bad" for defining constants because the compiler can't check types then.
On the other hand, if one line of macro saves you 20 lines of explicit code, some people will certainly agree that it's useful even if it's inherently unsafe. That's because writing more code usually implies higher probability for mistakes.
This question already has answers here:
Why statements cannot appear at namespace scope?
(3 answers)
Closed 5 years ago.
I don't know if I am just using the wrong keywords.. but I can't find an answer on google. Not can I wrap my feeble mind around my error.
This is a simple demonstration of the error:
#include <iostream>
//std::cout << "hello";
int main()
{
std::cout << "hello";
return 0;
}
Upon compiling/running this I receive this error:
main.cpp:3:6: error: 'cout' in namespace 'std' does not name a type
However, if I remove the first cout line, and just allow the program to execute the one inside of the main function, it works just fine.
Anyone got any ideas?
You cannot run code outside functions in C++. The compiler only expect variable declarations outside functions and, thus, expects std::cout to be a type, which it is not.
This question already has answers here:
Why does this work: returning C string literal from std::string function and calling c_str()
(9 answers)
Closed 8 years ago.
Please look at this code.
#include <iostream>
#include <string>
using namespace std;
int main() {
string hello = "Hello"
, world = "World";
const char *p = (hello+world).c_str();
cout << "STRING: " << p <<endl;
return 0;
}
I have no reputation, can't post images so that I will write results by hand.
= Visual Studio 2013 ver.12.0.30110.00
STRING:
= Dev-C++ ver.4.9.9.2
STRING: HelloWorld
The first following is execution result that compiled by Visual Studio.
Second is compiled by Dev-C++.
I wonder what makes this difference.
I will be looking forward to your reply. Thanks :)
(hello+world).c_str() is only valid until the trailing ;. Accessing the memory afterwards is undefined behavior.
Visual studio probably actually clears the memory, Dev-C++ doesn't bother. Try building a release version with Visual studio (optimizations on) and you'll probably see the same behavior.