Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default variable value

Tags:

c++

If I don't assign a value to a variable when I declare it, does it default to zero or just whatever was previously in the memory?

e.g.

float x; 
like image 520
CaptainProg Avatar asked May 17 '11 14:05

CaptainProg


People also ask

What is a default value example?

For instance, if a bank's interest rate for the current financial year is 8 percent, this may be set as the default value. This relieves the user from having to enter it repeatedly into the database.

Which are default values?

A default value is the value that is inserted into a column when an explicit value is not specified in an INSERT statement. A default value can be a literal character string that you define or one of the following SQL constant expressions: USER. CURRENT.

How can we set default value to the variable?

You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.

What is the default variable type?

The default value of a variable declared with var or let is undefined, UNLESS you initialize it to a value, in which case, it gets the type of the value.


1 Answers

A declared variable can be Zero Initialized, Value Initialized or Default Initialized.

The C++03 Standard 8.5/5 aptly defines each:

To zero-initialize an object of type T means:

— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject
is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:
— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized

For example:

#include<iostream> using namespace std;  static int a; //Zero Initialized int b; //Zero Initialized  int main() {     int i;  //Undefined Behavior, Might be Initialized to anything     static int j; //Zero Initialized      cout<<"\nLocal Uninitialized int variable [i]"<<i<<"\n";      cout<<"\nLocal Uninitialized Static int variable [j]"<<j<<"\n";      cout<<"\nGlobal Uninitialized Static int variable [a]"<<a<<"\n";      cout<<"\nGlobal Uninitialized int variable [b]"<<b<<"\n";      return 0; } 

You will notice The results for variable i will be different on different compilers. Such local uninitialized variables SHOULD NEVER be used. In fact, if you turn on strict compiler warnings, the compiler shall report an error about it. Here's how codepad reports it an error.

cc1plus: warnings being treated as errors In function 'int main()': Line 11: warning: 'i' is used uninitialized in this function 

Edit: As rightfully pointed out by @Kirill V. Lyadvinsky in the comments, SHOULD NEVER is a rather very strong word, and there can be perfectly valid code which might use uninitialized variables as he points out an example in his comment. So, I should probably say:
You should never be using uninitialized variables unless you know exactly what you are doing.

like image 120
Alok Save Avatar answered Oct 01 '22 10:10

Alok Save