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.
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:
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:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Does a const reference class member prolong the life of a temporary?
(6 answers)
Closed 5 years ago.
i have some sample code that behaves different in gcc(6.3) and in MSVC (11). I know that passing raw "hello" is not going to last when i call std::cout. In Microsoft Visual Studio 11 i get the expected output but in gcc the string remains in memory. The problem is that the code in gcc may compile, may behave as expected but is wrong, and i do not know why did not erase the "hello" string from memory. What i am missing? (Ignore the memory leaks is just dummy code)
Edit
The question is more oriented about the difference of compiling with gcc or MSVC. I have already pointed that the code will yield into undefined behaviour. The point is that something like this has been running ok under linux during years and has crashed at first run under a Windows port. Maybe that was only luck but i was asking more about gcc compiler doing nasty things in the background.
MSVC output
w_holder:
Gcc output
w_holder:hello
Code
#include <iostream>
#include <vector>
#include <string>
class StringWrapper
{
public:
StringWrapper(const std::string& sInput) : _string(sInput) { }
const std::string& _string;
};
class WrapperHolder
{
public:
WrapperHolder(const std::string& sInput) { _swrapper = new StringWrapper(sInput); }
StringWrapper* _swrapper;
};
int main()
{
WrapperHolder* w_holder = 0;
w_holder = new WrapperHolder("hello");
std::cout << "w_holder: " << w_holder->_swrapper->_string << std::endl;
return 0;
}
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.
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.