Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between various initializers in C++

Tags:

c++

c++11

I've only recently started learning C++ as part of my 10th Grade syllabus, and am only aware of the basics, thus simple answers (if possible) will be appreciated. I'm rather confused between initialization and assignment.

//Case 1
int a=5; //This is initialization
a=6; //This is assignment

From what I've understood, a variable is initialized when you give it a value to hold while declaring it. Changing this later in the code will be an assignment. Right?

What about :

//Case 2
int b;
{
//Block of code which does not call variable b
.
.
.
//End of block
}
b=6; // Is this initialization as well?

While 'b' is uninitialized when we declare, we later assign the value '6'. Can we say the 'b' is initialized now? Or are the terms initialized and uninitialized not applicable to 'b' anymore?

I read the an uninitialized variable holds "garbage values" till it isn't initialized. What exactly are "garbage values"?

What is the difference between the following initializers : '()', '{}', and '='?

like image 856
Cysearo Avatar asked Aug 23 '15 08:08

Cysearo


People also ask

What are initializers in C?

Initializer. In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces).

How many types of initialization are there?

Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.

What is the difference between variable declaration and variable initialization?

Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it. Initialization is the process of assigning a value to the Variable.

What is the difference between initialization and assignment?

What is the difference between initialization and assignment? Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.


2 Answers

Okay, once you declare a variable without assigning any value, like

int b; 

that means that the compiler reserves some space in the memory to hold the value (to be exact, in this case the memory is reserved on the stack). But since you didn't assign any value to the variable, it still holds the value, that the assigned space in memory had before. And that can be anything. Those are garbage values.

Initializers:

int b(1);

assigns the value 1 to be (in general, it calls a constructor of the type)

The brackets can be used to initialize arrays like this (edit):

int b[] = {1, 3, 5, 7};

And the = just assigns a value. The difference between this and the first will only become interesting when dealing with more complex types (classes), where you have constructors

like image 166
PeterO Avatar answered Sep 28 '22 08:09

PeterO


Easilly spoken:

Uninitialize variable:

 int a;

You are declare a variable that means you allocate memory but dont assign a value to it. So its compiler dependend if the value is set to 0 or not. So there could be anything in. Thats waht you called garbage values.

Initialized variable:

int a = 0;

You are declare a variable that means you allocate memory and assigne a value to it.

Assigne Values:

a = 10;

You assigne a rvalue (in this case 10) to a lvalue ( a). So you dont allocate new memory.

like image 43
ratnim Avatar answered Sep 28 '22 09:09

ratnim