V
V
Vladimir Mironov2015-11-28 19:16:42
Programming
Vladimir Mironov, 2015-11-28 19:16:42

Why are created variables not null in c++?

Hey!
There is this code in c++:

#include <iostream>
using namespace std;

int main()
{
  int a,b,c,d;

  cout << "a = " << a << "\n"; // a = 379757328
  cout << "b = " << b << "\n"; // b = 32764
  cout << "c = " << c<< "\n"; // c = 0
  cout << "d = " << d << "\n"; // d = 0

  return 0;
}

And I don't understand why the variable a and b is equal to some number?
In this case, the other two variables are equal to zero! I have no idea why that is.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kovalsky, 2015-11-28
@Siseod

Because you considered the chapter of the C ++ textbook about creating variables and initializing them to be nonsense. When creating a variable on the stack in C++, the memory location that the variable looks at does not change its value. Therefore, there is garbage to meet at times. if the initial value of the variable is important to you - initialize the variable MANUALLY)

F
fuzz0, 2015-11-28
@fuzz0

use universal initialization:
now a = 0

D
Dmitry, 2015-11-29
@TrueBers

Learn assembler. After him, such questions cannot arise in principle.
Learn what a stack is, how it works, how it grows, how memory is allocated on it. It's better to use a visual debugger for this, such as OllyDbg or x64dbg.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question