- What is a variable in C++?
A named object, and the name of the object is called IDENTIFIER. And an object in C++ is a region of the memory, that has a VALUE and other properties associated with it.
- What is the Definition of a variable?
its a special type of declaration statement, where the variable is first presented into the program. It is used to CREATE variables.
- What is Instantiation of a variable?
It is the process in which the variable will become an object and the object will be assign a memory address. The instantiation happens at RUNTIME (when the program is ran), whilst the definition of a variable happens at COMPILE TIME (when the program is compiled)
- What is the difference between an l-value and an r-value?
In C, definition if lvalue and rvalue was somewhat simple, anything i.e. left of assignment operator is lvalue and right of assignment operator is rvalue. But in C++ this definition has changed and become more interesting.
lvalue is anything whose address is accessible. It means we can take address of lvalue using & operator.
Rvalue is anything that is not lvalue. It means we cannot take address of rvalue and it also don’t persist beyond the single expression.
- What is an uninitialized variable and what kind of behavior can you expect from such a variable?
it is when you define a variable, but you don´t give it an initial value. For expample:
int x;
you can expect that any value could be assigned to that variable, since a “free” memory space will be assigned, but its actually not free, since it will have whatever content it had before it was assigned to store the value of X. We can expect undefined behavior by using uninitialized variables, and this should be avoided at all cost. For example, what will this code print to the standard output?:
#include <iostream>
int main() {
int x;
std::cout << x;
return 0;
}
- What is undefined behavior?
Undefined behavior (often abbreviated UB) is the result of executing code whose behavior is not well defined by the C++ language. In this case, the C++ language doesn’t have any rules determining what happens if you use the value of a variable that has not been given a known value. Consequently, if you actually do this, undefined behavior will result.
Code implementing undefined behavior may exhibit any of the following symptoms:
*** Your program produces different results every time it is run.**
*** Your program consistently produces the same incorrect result.**
*** Your program behaves inconsistently (sometimes produces the correct result, sometimes not).**
*** Your program seems like its working but produces incorrect results later in the program.**
*** Your program crashes, either immediately or later.**
*** Your program works on some compilers but not others.**
*** Your program works until you change some other seemingly unrelated code.**
Or, your code may actually produce the correct behavior anyway. The nature of undefined behavior is that you never quite know what you’re going to get, whether you’ll get it every time, and whether that behavior will change when you make other changes.
C++ contains many cases that can result in undefined behavior if you’re not careful. We’ll point these out in future lessons whenever we encounter them. Take note of where these cases are and make sure you avoid them.