Variables in C++ - Reading Assignment

  1. What is a variable in C++?

A variable in C++ is an object, i.e. a piece of memory, with a name associated to it.

  1. What is Definition of a variable?

The Definition of a variable is a statement that when executed will trigger the Instantiation of said variable.

  1. What is Instantiation of a variable?

The Instantiation of a variable consists in setting aside a piece of memory associated to this variable.

  1. What is the difference between an l-value and an r-value?

A l-value is associated with a persistent memory address while a r-value isn’t.

  1. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?

An uninitialized variable is a variable that has not be given any value after being defined. The variable is associated to a piece of memory and its value is then whatever resides in that piece of memory.

  1. What is undefined behaviour?

An undefined behaviour is the result of executing a piece of code that has no defined behaviour in the language.

  1. What is a variable in C++?
    

A chunk of memory allocated in RAM

  1. What is Definition of a variable?
    

defining it’s name and it’s data type

  1. What is Instantiation of a variable?
    

giving a value to a variable

  1. What is the difference between an l-value and an r-value?
    

l-value stays in memory, r-value is temporary and discarded after it’s used

  1. What is an uninitialized variable and what kind of behavior can you expect from such a variable?
    

a variable that has not had a value assigned to it, your program most likely won’t compile

  1. What is undefined behavior?
    

odd behavior that happens when an uninitiated variable is used.

  1. A variable in C++ is simply an object (a place in memory) that has a name.
  2. A definition is a declaration statement used to create a variable.
  3. Instantiation is setting aside memory in RAM (for a variable)
  4. An l-value is a value that has a persistent address (in memory). An r-value refers to values that do not have a persistent address in memory.
  5. An uninitialized variable is a variable which has not been assigned a value in the program. Undefined behaviour may result from this.
  6. Undefined behavior occurs when the program implements code whose behaviour is not well defined by the language. When code with undefined behaviour is implemented, the program may exhibit unexpected results.

What is a variable in C++?
A name that identify a value in memory

What is Definition of a variable?
A statement to define a variable

What is Instantiation of a variable?
Give a name to a memory address

What is the difference between an l-value and an r-value?
l-value has space in memory (variables)

What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
It is a variable without assignment, and this can lead to undefined behavior because the variable can take any value.

What is undefined behaviour?
Behavior that is not defined by the language and can lead to unexpected situations.

What is a variable in C++?
simply an object that has a name.

What is Definition of a variable?
specifying a variable and declaring its type.

What is Instantiation of a variable?
defining and assigning a value to a variable.

What is the difference between an l-value and an r-value?
l-value has a permanent location in memory - usually the left side of an assignment.
r-value is not permanent in memory and is discarded after use
 usually on the right side on and assignment.

What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
int x;
this variable points to a location in memory and that memory contains whatever was previously there.

What is undefined behaviour?
is the result of executing code whose behavior is not well defined by the language. an example of this is an unintialized variable.

1. What is a variable in C++?
It’s an object that has a name and through this name is possible access a piece of memory where this object is storage.

2. What is Definition of a variable?
It’s an special kind of statement that create a variable.

3. What is Instantiation of a variable?
When a definition statement is executed, a piece of memory from RAM is reserved and the address of this piece is linked with the variable.

4. What is the difference between an l-value and an r-value?
An l-value has a persistent address like all variables, whereas r-value is the value that are not associated with the persistent memory address.

5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
It’s a variable that has not been given a known value through initialization or assignment. An uninitialized variable can has any value hold in piece of memory allocated for it, these way, this kind of variable can result an undefined program behavior.

6. What is undefined behaviour?
It’s the result of executing code whose behavior is not well defined by the language.

What is a variable in C++?
variable is an object that has a name
What is Definition of a variable?
definition is a special kind of declaration statement that we use for create a variable. we need to define a type a of a variable and its name ( int a; )
What is Instantiation of a variable?
instantiation is reserving a part of memory for our variable.
What is the difference between an l-value and an r-value?
l-value represents a memory address. it is on the left side of the assignment operator. r-value is value assigned to a variable (l-value) and it is on the right side of the assignment operator.
in example y=3; a is l-value (variable) and 3 is r-value assigned to a variable y.
What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
it is a variable without a value. every time we run the program, our variable has a different value. modern compiler can write warning that we didn’t initialize a variable.
What is undefined behaviour?
it is when something in the code is not well defined. i.e. uninitialized variable

  1. A variable is an object with a name
  2. Declaration of a variable is called as defining the variable eg: int x
  3. Instantiation of a variable is assigning some address to variable in the memory
  4. I-value is the persistent value it remains when it’s created whereas r-value is the temporary value which is created only for that moment and vanishes later
  5. A uninitialized variable is the one which has a variable whose value is not assigned but it has a garbage value which creates an error when later referred in the code
  6. An undefined behavior causes an unexpected feature in the program or it creates uncertainty in the program like an uninitialized variable.

What is a variable in C++?
a variable is a name for an object that can contain something (for example a string)
What is Definition of a variable?
Defining a variable means that we tell the program that we want to use it so we have to create it first
What is Instantiation of a variable?
a piece of the memory is assigned to that variable
What is the difference between an l-value and an r-value?
all l-values have a memory address assigned, so all variables are l-values. r-values are not assigned to a memory address and are temporarily needed while executing a statement to hold some temporariy value before it is assigned to a variable.
What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
This is a variable that did not get a default value when you create is. You never know what you will get from that variable since it just returns whatever is inside the mem adres it gets allocated to.
What is undefined behaviour?
All kinds of behaviour of a program that the language doesn’t know what to do with is (not defined in the language)

What is a variable in C++?
A variable it is simply an object that has a name. That name points precisely to a
part of the memory.

What is Definition of a variable?
Give it a name so that it is allocated in a specific part of the memory. For instance
int x;
defines a variable, but it is not initialized.

What is Instantiation of a variable?
Create a variable AND give it an initial value. For instance
int x=5;
initialized the variable x with the initial integer value of 5.

What is the difference between an l-value and an r-value?
l-values are permanent allocations in the memory while r-values just last for the time they are used to perform the (righthand side of the) code they are in.
Thinking back to the lecture, I imagine r-variables are allocated in the MEMORY and r-variables are allocated in the ALU (arithmetic logic unit).

What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
It is a variable that is defined but it is not given right away a value. Such variables are troublesome because although they are not GIVEN an initial value, the value that they have
happens to be the last value that the memory “remembers” for them.
Since you do not control the value of an uninitialized variable, the code runs with whatever the value happens to be at that time and can result in:
(a) wrong answer.
(b) right answer, but sometimes, not always.
© the code might work with some compilers and not others.

Since many things can go wrong, such variables are difficult to debug.

What is undefined behaviour?

Is the result of running code with uninitialized variables.

Quiz
My answers:
Ans1. 3
Ans2. 3
Ans3. 6
Ans4. 3
Ans5. whatever happens to be in the memory that z points to.

1. What is a variable in C++?
Variable in C++ is an object which has a name.
2. What is Definition of a variable?
Is an expression saying what type the variable is and what is its name.
3. What is Instantiation of a variable?
Instantiation is setting the variable a value.
4. What is the difference between an l-value and an r-value?
l-value has a persistent address in memory and the r-value is not assigned with the persistent address in memory.
5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
Uninitialized variable is the one which has not been given a value. Such variable has not assigned a space in memory and it can lead to undefined behavior.
6. What is undefined behavior?
Undefined behavior is the result of executing the code whose behavior is not specified by the language.

  1. A variable is an object (which is a place in memory where data can be stored) that has a name.
  2. Definition of variable is when the variable is simply declared with its type so the required memory is allotted for it but no data is stored into it.
  3. Where the variable is given its memory location.
int x; // instantiation
  1. l-value is the value which has persistent memory address and values can be assigned to it.
    r-value is the value which doesnt have persistent memory address and is used during evaluation and then discarded.
  2. Variable which hasn’t been given a definite value to hold in the program. Undefined behaviour is expected.
  3. Where the outcome is undefined.
  1. As the text says: an object with a name.
  2. Is when we tell the code what kind of variable we are using.
  3. Is when we reserve a part of the RAM to hold the variable.
  4. l-values exist in the memory and can be manipulated as we wish. r-values are not associated with the memory, are constant by default and cannot be changed. The names come from where is common to find those values, since l-values get assigned by placing them on the left side of the assignment, and r-values always come in the right side of the assignment.
  5. Is a variable that has no value printed on the assigned memory location. We just can’t predict what is going to happen, since when we call that variable, is going to display whatever that resided on that memory block or simply an error.
  6. An unexpected, commonly random, outcome of the code. It may usually happen when we have issues as having uninitialized variables.

What is a variable in C++?
A memory location.
What is Definition of a variable?
The line where it is declared along with it’s type.
What is Instantiation of a variable?
Where it is assigned a value consistent with its type.
What is the difference between an l-value and an r-value? One has a persistent addy in memory, the other does not.
What is an uninitialized variable and what kind of behaviour can you expect from such a variable? inconsistent behavior, it will contain whatever artifacts were already in memory.
What is undefined behaviour? who knows. It might not be what you think

  1. A variable is an object with a name. It is used as a placeholder for data such as integers and other types.
  2. Definition of a variable is when you are declaring a variable with a name and a value. For example:
    int x; // creates a integer variable named x
  3. Instantiation is when your program is executed and a memory (RAM) piece is set aside for each variable.
  4. The difference is that l-values have a persistent address value in memory, usually variable names. On the other hand, r-values are usually numbers or expressions that do not have an address in memory.
  5. An uninitialized variable is a variable that has not been assigned a value in code. This causes the compiler to assign it whatever garbage value was there before. This will severely alter code if not fixed.
  6. Undefined behavior is when you execute your program using code that is not well defined. This may happen from using a value of an uninitialized variable.

A variable is a named location in memory.

You make a definition to create (define) a variable. When the computer executes the definition, it instantiates (sets memory aside for) the variable.

An l-value has a persistent address in memory, while an r-value doesn’t. L-values and r-values go on the left and right sides of assignment operations, respectively.

An uninitialized variable is a variable that you didn’t initialize (assign an initial value to when defining) or assign a value to later. Uninitialized variables can result in undefined behavior (behavior that is not well-defined in the applicable language and/or library specifications, reference manuals, etc.).

1. What is a variable in C++?
A variable in C++ is simply an object that has a name.

2. What is Definition of a variable?
In order to create a variable, we generally use a special kind of declaration statement called a definition

3. What is Instantiation of a variable?
When this statement is executed by the CPU, a piece of memory from RAM will be set aside (called instantiation)
int y;

4. What is the difference between an l-value and an r-value?
An l-value is a value that has a persistent address (in memory). Since all variables have addresses, all variables are l-values. r-values are generally temporary in nature and are discarded at the end of the statement in which they occur.
y = 4; // r-value 4 evaluates to 4, which is then assigned to l-value y
y = 2 + 5; // r-value 2 + r-value 5 evaluates to r-value 7, which is then assigned to l-value y

5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
A variable that has not been given a known value (through initialization or assignment) is called an uninitialized variable.

6. What is undefined behaviour?
Using the value from an uninitialized variable is our first example of undefined behavior. Undefined behavior is the result of executing code whose behavior is not well defined by the language

7. Output on my system when running sample code.
3
3
6
3
4354256 // garbage in this memory location as no value assigned previously

1. What is a variable in C++?

A variable in C++ s simply an object that has a name.

2. What is Definition of a variable?

The definition of a variable refers to name of a variable and placed to a data type.

_3. What is Instantiation of a variable? _

The instantiation of a variable is when a statement is executed by a CPU, thus enabling
for a section of the RAM (random access memory) to be set aside. In doing so, the statement can be recalled later.

4. What is the difference between an l-value and an r-value?

An l-value refers to the value that maintains a persistent address in memory while, an r-value refers to the value of a temporary address that is discarded once it deployed.

5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?

An uninitialized variable refers to a variable that will return a value from a previous session that is stored in memory. The behavior of the uninitialized variable can result in errors if the variable’s value is different from the variable is a previous session.

6. What is undefined behaviour?

Undefined behavior is when the program executes errors due a variable being declared without an assigned value.

1. What is a variable in C++?

A variable in C++ is an object that has a name.

2. What is Definition of a variable?

A Definition of a variable is a special kind of declaration statement.

3. What is Instantiation of a variable?

Instantiation of a variable is a piece of memory from RAM set aside, when a statement is executed by the CPU.

4. What is the difference between an l-value and an r-value?

An l-value is a value that has a persistent memory address.

An r-value is a value that doesn’t have a persistent memory address.

5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?

An uninitialized variable is a variable, which hasn’t been given a known value via initialization or assignment.

You can expect unexpected results from such a variable.

6. What is undefined behaviour?

Undefined behavior is the result of executing code whose behavior isn’t defined by the language.

  1. What is a variable in C++?
    A variable in C++ is simply an object that has a name.

  2. What is Definition of a variable?
    In order to create a variable, we generally use a special kind of declaration statement called a definition. E.g. “int x” is the definition for an integer variable named x.

  3. What is Instantiation of a variable?
    When the statement “int x” is executed by the CPU, a piece of memory from RAM will be set aside (called instantiation).

  4. What is the difference between an l-value and an r-value?
    In C++, variables are a type of l-value (pronounced ell-value). An l-value is a value that has a persistent address (in memory). Since all variables have addresses, all variables are l-values. The name l-value came about because l-values are the only values that can be on the left side of an assignment statement. The opposite of l-values are r-values (pronounced arr-values). An r-value refers to values that are not associated with a persistent memory address. Examples of r-values are single numbers (such as 5, which evaluates to 5) and expressions (such as 2 + x, which evaluates to the value of variable x plus 2). r-values are generally temporary in nature and are discarded at the end of the statement in which they occur.

  5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
    C++ will let you both define a variable AND give it an initial value in the same step. This is called initialization.
    int x = 5; // initialize variable x with the value 5
    Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is assigned a memory location by the compiler, the default value of that variable is whatever value happens to already be in that memory location. A variable that has not been given a known value (through initialization or assignment) is called an uninitialized variable. Using uninitialized variables is one of the most common mistakes that novice programmers make, and unfortunately, it can also be one of the most challenging to debug.

  6. What is undefined behaviour?
    Using the value from an uninitialized variable is a first example of undefined behavior. Undefined behavior is the result of executing code whose behavior is not well defined by the language. Code implementing undefined behavior may exhibit any of the following symptoms:

  • Your program produces an consistently incorrect result.
  • Your program produces different results every time it is executed.
  • Your program behaves inconsistently.
  • 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 unrelated code.
    Or, your code may actually produce the correct behaviour anyway. The nature of undefined behaviour is that you never quite know what you’re going to get, whether you’ll get it every time, and whether it’ll change when you make other changes.