13

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 '='?

Sam Hosseini
  • 813
  • 2
  • 9
  • 17
Cysearo
  • 141
  • 8

4 Answers4

10

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

PeterO
  • 470
  • 3
  • 12
  • Thanks for the explanation about garbage values and initializers. Could you please tell me if Case 2 is an example of initialization as well? Also, what are constructors which you mentioned earlier? – Cysearo Aug 23 '15 at 08:48
  • Yes, case 2 would be initialization. Regarding the constructor, that would be quite a big topic. When you want to read about it, you can find plenty of tutorials on the internet, like this one: http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm – PeterO Aug 23 '15 at 08:55
  • 5
    Um, no. Case 2 is an assignment. Initialisation can only happen at a point of definition (except for init-bases-members). – n. m. could be an AI Aug 23 '15 at 09:10
  • `int[] b`? I think you're bit confused – David Haim Aug 23 '15 at 12:45
  • @n.m. So initialization only occurs when you give a variable a a value when you declare it? From what I've gathered, init-base is a class, but I couldn't find how the initialization of an init-base is different from that of a regular variable. Could you please tell how? – Cysearo Aug 23 '15 at 13:14
  • @DavidHaim I believe you mean that it should be int b[]. In this case 'b' would be an integer array and the brackets would hold it's size, right? Sorry, if the doubt seems trivial but from my experience so far, one can't take anything for granted while learning C++... – Cysearo Aug 23 '15 at 13:18
  • Sorry, confused the brackets, I have edited it to be correct: int b[] instead of int[] b; And yes, it would be an array of integers and the brackets hold it's size, but if you assing values with {} you can just drop the size, because it gets clear by the number of values within the curly brackets {} – PeterO Aug 23 '15 at 14:30
  • @user3468249 you should mention `const` to further contrast the difference between initialization and assignment. – transistor09 Aug 23 '15 at 16:04
  • 1
    It's a little misleading to say that declaring a variable reserves memory. It does no such thing. It declares the name. The contents are not defined and do not exist as far as the standard is concerned. It certainly is not true that the variable contains garbage at this point. It does not contain anything observable. – usr Aug 23 '15 at 18:45
  • "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)" - given that compiler is usually known to employ various optimization techniques the exact statement is not necessarily exact and may mislead later during learning - for example in C [read from uninitialized variable is undefined behaviour](https://stackoverflow.com/questions/11233602/what-will-be-the-value-of-uninitialized-variable) . I'm quite sure that applies to C++ as well but I cannot find any reference to it ATM. – Maciej Piechotka Aug 23 '15 at 18:51
  • (usr beat me while I was looking for reference - sorry for repeating and +1) Even with "benign" compiler on some architectures (like Itanium) read will be a trap value. Modern compilers will probably assume that never happens which may result in interesting debugging sessions with haisenbugs. – Maciej Piechotka Aug 23 '15 at 18:54
  • @usr, so declaration doesn't reserve memory? But definition does? What if it's a definition without an initialization? – Cysearo Aug 24 '15 at 10:00
  • @Cysearo a definition does not reserve memory either. The compiler can do what it wants to as long as it appears as if the abstract machine was working. Nowhere does the standard say that a variable reserves memory as soon as it is assigned a value. As a practical example if you say ´int i = 1234;` that's a constant that will disappear during optimization. It will be propagated forward. – usr Aug 24 '15 at 10:24
  • Maxbe you should say "Does not NECESSARILY reserve memory". This was a very basic question and we should not overcomplicate it by dealing with various optimization techniques of different compilers!!! This will be more confusing then helpfull! – PeterO Aug 24 '15 at 11:19
  • @PeterO the problem is that it has practical differences - if you think that variables are backed by memory you may think many operations are safe, when they really aren't. For example in C-backed-by-memory-model `float f = ...; int i = *(int *)&f` should, work while in C-as-abstract-machine-defined by standard it does not. – Maciej Piechotka Aug 24 '15 at 15:32
5

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.

ratnim
  • 129
  • 7
5

You're basically right.

Some older texts call the first assignment to an uninitialised variable an "initialisation", although this is not strictly accurate.

"Garbage values" are arbitrary values. They could look meaningful or could be totally random.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

Initialization serves to initialize an uninitialized value.

It can be done my means of copy constructor, i.e. int a = 1; or int a(1);, it can be done by means of assignment, i.e. int a; a = 1;, it can be done via a function, i.e. int a; init(a);. Initialization is not a "language thing", it is just the act of specifying an unspecified value.

A "garbage value" is an arbitrary value. Some storage will be given to the uninitialized object, and attempting to read it will produce a value of whatever happened to be in that memory.

dtech
  • 47,916
  • 17
  • 112
  • 190