Set values or not in c++variables

Asked

Viewed 745 times

7

Good night. Should I or should I not set values in variables when creating them in c++? An example:

int x = 0;
int y;

What’s the difference in these two cases? And what happens in memory when I initialize it with 0 and without it.

2 answers

6


In the presented case, both variables would be initialized with the value 0 (in the first case explicitly and, in the second, because it is the missing value for variables of the type in question).

However it is important to stress the following in a succinct way: assigning values to variables at the time of their declaration is a good practice, since the process of declaring it implies that it will point to a certain location of the memory. When you access the variable, the program will return the value stored in that location (in many cases it is simply junk). If the variable is not initialized, it is possible that any undetermined value will appear a priori and not its supposed omitted value.

Pay attention to this example:

#include <iostream>
using namespace std;

struct teste{
    int variavel;
};

int main(){

    teste a;
    cout << a.variavel;
    return 0;

}

In my case, the result returned was 36 because it was the value that happened to be stored in the particular memory location and not the default value of 0. In your case it will surely be someone else.

In short, some advantages will be:

  • allows greater readability of the code as well as better maintenance of it
  • avoids that possible errors of execution may arise because they are not properly initialized, as well as some errors because an undetermined value is assigned
  • 1

    Just one consideration: the value in the variable will not necessarily be exactly what was in the memory position. The value is simply undetermined. The compiler is free to put whatever he wants in there. The compiler may even put specific values there when compiling in debug mode, for example, to help find bugs.

2

Global variables or static variables not initialized by the programmer are initialized by the compiler with the default constructor. In practice, variables of the "native" types (int, char, bool, etc.) are initialized with 0, converted to the type of the variable (e. g: for bool type, 0 is converted to false).

PS. "native" types are usually classified as "POD" (Plain Old Datatype).

#include <iostream>
using namespace std;

int x = 0;
int y;

int main()
{
   cout << "x=" << x << endl; // zero
   cout << "y=" << y << endl; // zero
}

Nonstatic local variables not initialized by the programmer are initialized by the compiler with the default constructor, with the exception of POD variables (see above what is POD).

In practice this means that for non-static local variables POD not initialized the initial value is unpredictable ("trash").

#include <iostream>
using namespace std;

int main()
{
   int x = 0;
   int y;
   cout << "x=" << x << endl; // zero
   cout << "y=" << y << endl; // ???
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.