Home > Software design >  How the compiler decides the value and data type of variable b here?
How the compiler decides the value and data type of variable b here?

Time:01-15

I am unable to understand how the memory allocation is working for variable b , is there some logic behind it or its just an another UB . The data type of b also becomes integer. \

int a = 5,b;
cout<<b; // 16

CodePudding user response:

You can declare several variables in a single line. b is declared to be an int. It is not initialized. Using its value is undefined behavior, because it is uninitialized, it has an indeterminate value.

The recommendation is to declare a single variable per line and to always initialize variables:

int a = 5;
int b = 0;
std::cout << b;   // prints 0

In a single line that would be

int a = 5, b = 0;
std::cout << b;  // prints 0

CodePudding user response:

How the compiler decides the value and data type of variable b here?

The compiler interprets the data type from the source code. You declared it as int, so that is the type.

You didn't provide any initialiser, so the value is indeterminate. When you read the indeterminate value upon inserting it to the character stream, the behaviour of the program is undefined. Don't do this.

CodePudding user response:

The C standard does not define what happens with this program: the behaviour on reading an uninitialised int is undefined.

If you want to examine the compiled program (to see what's going to happen), then look at the compiled program. Bear in mind though that what you see is only one possibility of many. Note that modern optimising compilers will often reason to themselves that std::cout << b; is not reachable as they are allowed to assume that there is no undefined branch, and compile out that branch altogether.

CodePudding user response:

When you wrote:

int a = 5,b;

The above statement means four things:

  1. You're defining 2 variables named a and b.
  2. The type of these variables is int because you've specified the type as int.
  3. The variable a has been initialized with the prvalue expression 5. This means that from this point onwards a holds the value 5.
  4. The variable b has not been initialized. This means that b holds an indeterminate value. This has an important consequence in your program. In particular, if/when you use this uninitialized variable b then it will result in undefined behavior. So when you wrote:
cout<<b;//this is undefined behavior

The above statement leads to undefined behavior because you're using the uninitialized variable b.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing is a result of undefined behavior. And as i said don't rely on the output of a program that has UB.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.

Solution

This is the reason it is advised that:

always initialize built in types in local/block scope.

So you can solve this problem by initializing the vairalbe b as shown below:

int a = 5, b = 3; //initialize a with 5 and b with 3
std::cout << b;   //this is valid now

1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

CodePudding user response:

when you define a local variable without assignment, the value of it is not certain because the memory of the variable is distributed in stack area, and the data in stack are rubbish value.

In my environment, the run result is 0;

  •  Tags:  
  • Related