Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialisation and assignment

Tags:

What EXACTLY is the difference between INITIALIZATION and ASSIGNMENT ?

PS : If possible please give examples in C and C++ , specifically .

Actually , I was confused by these statements ...

C++ provides another way of initializing member variables that allows us to initialize member variables when they are created rather than afterwards. This is done through use of an initialization list. Using an initialization list is very similar to doing implicit assignments.

like image 248
progammer Avatar asked Sep 08 '11 14:09

progammer


People also ask

What is the difference between initialization and declaration?

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. Every programming language has its own method of initializing the variable.

What is the difference between assignment and initialization in Java?

initialization: to initialize a variable. It can be done at the time of declaration. assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.

What does initialization mean?

: to set (something, such as a computer program counter) to a starting position, value, or configuration.

What are the two types of initialisation?

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.


1 Answers

Oh my. Initialization and assignment. Well, that's confusion for sure!

To initialize is to make ready for use. And when we're talking about a variable, that means giving the variable a first, useful value. And one way to do that is by using an assignment.

So it's pretty subtle: assignment is one way to do initialization.

Assignment works well for initializing e.g. an int, but it doesn't work well for initializing e.g. a std::string. Why? Because the std::string object contains at least one pointer to dynamically allocated memory, and

  • if the object has not yet been initialized, that pointer needs to be set to point at a properly allocated buffer (block of memory to hold the string contents), but

  • if the object has already been initialized, then an assignment may have to deallocate the old buffer and allocate a new one.

So the std::string object's assignment operator evidently has to behave in two different ways, depending on whether the object has already been initialized or not!

Of course it doesn't behave in two different ways. Instead, for a std::string object the initialization is taken care of by a constructor. You can say that a constructor's job is to take the area of memory that will represent the object, and change the arbitrary bits there to something suitable for the object type, something that represents a valid object state.

That initialization from raw memory should ideally be done once for each object, before any other operations on the object.

And the C++ rules effectively guarantee that. At least as long as you don't use very low level facilities. One might call that the C++ construction guarantee.

So, this means that when you do

    std::string s( "one" ); 

then you're doing simple construction from raw memory, but when you do

    std::string s;     s = "two"; 

then you're first constructing s (with an object state representing an empty string), and then assigning to this already initialized s.

And that, finally, allows me to answer your question. From the point of view of language independent programming the first useful value is presumably the one that's assigned, and so in this view one thinks of the assignment as initialization. Yet, at the C++ technical level initialization has already been done, by a call of std::string's default constructor, so at this level one thinks of the declaration as initialization, and the assignment as just a later change of value.

So, especially the term "initialization" depends on the context!

Simply apply some common sense to sort out what Someone Else probably means.

Cheers & hth.,

like image 155
Cheers and hth. - Alf Avatar answered Sep 27 '22 19:09

Cheers and hth. - Alf