-2

I’m testing a script using the floor() function on tutorialspoint’s codingground compiler. I start with this simple script:

#include <iostream>
#include <cmath>

using namespace std; 
/* Including std namespace since I am not including any other namespaces. */

int main() {
   double var1 = 3.393, var2;
   cout << var1 << “ “ << var2;
   return 0;
}

Result: 3.393 0. However, when I add floor() to var1, var2 changes to 6.95329e-310. Is there a reason for this, either that it is specific to the compiler or I’m doing something wrong? Resource: http://tpcg.io/SkgxtNCa

Jeffrey Lan
  • 90
  • 13
  • 6
    `var2` is uninitialized, you can't expect any value there. – 273K May 21 '21 at 16:52
  • 1
    Something wrong? At a guess: [you should always enable and address compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – JaMiT May 21 '21 at 16:54
  • 3
    Your program has _undefined behavior_. You can't expect anything specific. Be prepared for everything. (see: [_nasal demons_](http://catb.org/jargon/html/N/nasal-demons.html)) – Ted Lyngmo May 21 '21 at 16:57
  • 3
    Does this answer your question? [What happens to uninitialized variables? C++](https://stackoverflow.com/questions/40987901/what-happens-to-uninitialized-variables-c) – phuclv May 21 '21 at 17:04
  • 1
    @TedLyngmo Thanks, I understand now. – Jeffrey Lan May 21 '21 at 17:13
  • @JeffreyLan The code could have even crashed where you are trying to output the value of an uninitialized `double`. I have seen crashes occurring due to attempting to utilize uninitialized floating point variables. Also a C++ program is called a *program*, not a "script". – PaulMcKenzie May 21 '21 at 17:19

1 Answers1

1

var2 is uninitialized, it's a local variable which ends up on the stack.

When you call functions the stack is used to keep the return address and also the local variables of that function. When you don't initialize a variable it gets the last value left on the stack that was on that place. Some compilers might behave differently and some have flags for initializing. I guess the one you use doesn't do this and you will end up with a random result from what was on the stack, calling floor probably changed that.

Just change your code to initialize the variable

double var1 = 3.393, var2 = 0.0;

The comments below point out that it's not always on the stack, that's also right. Most implementations use a stack but it can be lot's of things depending on the platform/compiler. The most important thing is initialize your variables in unmanaged languages like c, c++, pascal, assembly (those are the ones I've used througout the years) because they won't do it for you unless you configure them to do so.

  • 3
    "_When you don't initialize a variable it gets the last value left on the stack_" - That's taking it too far. There is no stack required in C++. – Ted Lyngmo May 21 '21 at 16:59
  • 4
    The variable is not necessarily stored on the stack, it may also be stored only in a CPU register. This depends on the compiler and on the compiler settings, especially the optimization level of the compiler. – Andreas Wenzel May 21 '21 at 17:04