Variables in C++ - Reading Assignment

  1. What is a variable in C++?
    They are a statement of a value being bound
    An object that has a name (an object is a value stored in memory)

  2. What is Definition of a variable?
    The declarative statement ie; int x;

  3. What is Instantiation of a variable?
    A piece of RAM is set aside when a statement is executed

  4. What is the difference between an l-value and an r-value?
    An l-value has a persistent place in RAM and cannot equal things that aren’t variables such as the number 5. An r-value is basically the opposite; a value withough a persistent RAM location like 5

  5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
    An uninitialized variable is one that does not have an assigned value. This is when you do an int call but don’t follow up on it. The memory location associated with such a variable is random. Programs with uninitialized variables might still run fine, at parts so it can be hard to debug.
    Always initialize your variables

  6. What is undefined behaviour?
    Running a program with uninitialized variables produces behaviours that are different every time it runs, might seems like its running but eventually produces a poor result, crashes, your program could work fine until you change seemingly unrelated code

1 Like

1.What is a variable in C++?
A variable in C++ is an object, in which we assign a name to.

2.What is Definition of a variable?
When we define a variable, it is a kind of declaration, where we declare the variable type of the variable or define some other aspect of the variable.

3.What is Instantiation of a variable?
When we create a new variable in C++, a piece of memory in the Random Access Memory (RAM) is used to store this variable. This process is known as instantiation.

4.What is the difference between an l-value and an r-value?
(I remember them as l for left, and r for right). An l-value is one that has an address in the RAM. We can find it by searching within the memory. r-values on the other hand, do not have such associated addresses. They simply are. An example of an r-value would be the number 5. The number just is, and does not have a persistent memory address. As compared to if we declare the variable, numberFive like so:

int numberFive = 5;

In this case, numberFive has a persistent memory address, and thus is an l-value.

5.What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
An uninitialized variable is one that is created and not assigned/initialized any value. This can be troublesome. When called, the variable will randomly spit out the value stored in the particular memory location that the variable is in.

6.What is undefined behaviour?
Undefined behaviour appear, as symptoms/results of having undefined variables in the code. Some behaviours include:

  • Producing inconsistent, random results every time the code is run.
  • The program crashes abruptly
  • The program only works on certain compilers
  • The program only works when we change some other unrelated code
2 Likes
  1. What is a variable in C++?
    Is an object that has a name
  2. What is Definition of a variable?
    A declaration statement used in order to create a variable
  3. What is Instantiation of a variable?
    Reserving the needed memory (RAM) needed for a variable is called instantiation
  4. What is the difference between an l-value and an r-value?
    l-value has-persistent address & r-value is not associated with a persistent memory address.
  5. What is an uninitialised variable and what kind of behaviour can you expect from such a variable?
    A variable that has not been given a value is called uninitialised. The use of such a variable leads to unpredictable results as it’s value is being taken from the memory location it has been allocated to
  6. What is undefined behaviour?
    When a variable that is uninitialised is used in the code, then the bahavior of the program can be defined as “undefined behaviour”.
1 Like
  1. A variable in C++ is simply an object that has a name.

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

  3. When this statement is executed by the CPU, a piece of memory from RAM will be set aside (called instantiation).

  4. An l-value is a value that has a persistent address (in memory). 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).

  5. A variable that has not been given a known value (through initialization or assignment) is called an uninitialized variable. The behavior of the variable may change every time you run the program.

  6. Undefined behavior is the result of executing code whose behavior is not well defined by the language.

1 Like
What is a variable in C++?

a variable is an object that has a name.
What is Definition of a variable?
It’s a process that tells the compiler where to store and how much information is needed to create a variable.
What is Instantiation of a variable?
it’s when you define a value and give it initial value in the same setup.
What is the difference between an l-value and an r-value?
a l value is a value with a persistent address in memory, a r value is more temporary and assigned to addresses that may change.
What is an uninitialized variable and what kind of behavior can you expect from such a variable?
it’s when there is not a variable in place already so the compiler just uses anything that is available at the time.
What is undefined behaviour?
it’s when you put in a code the prints and error; something that doesn’t have defined variables resulting in an error.

1 Like

1 A variable in C++ is an object and a space in memory where values can be stored.
2 The definition of a variable is simply its creation along with a name.
3 Instantiation of a variable means the code parser fund an new variable and create a new space in memory for it.
4 The l-value is the name of the variable, the r-value is its value.
5 Uninitialized variables are variables with a l-value but no r-value which may cause unwanted results when the code is executed.
6 Undefined behaviours are behaviours programs have when one or more variables don’t have values understood by the language. If such values are used, the program will return random values that will make the execution wrong or unstable.

1 Like

Same here. Using xCode does the job so fare :raised_hands:

1 Like
  1. What is a variable in C++? It is an object with a memory address
  2. What is Definition of a variable? It creates/defines a variable
  3. What is Instantiation of a variable? The variable is assigned a particular memory address by the CPU
  4. What is the difference between an l-value and an r-value? The l-value is defined on the left side of the assignment statement (with a memory address), whilst an r-value is the right side value to be assigned to the l-value and then discarded once the statement is run.
  5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable? It is a variable that has been defined but has no value yet assigned to it. An uninitialised variable may inherit some ‘garbage’ data previously assigned to the same memory address.
  6. What is undefined behaviour? An example is using an uninitialised variable - it may result in unexpected behaviour
1 Like
  1. A variable in C++ is simply an object that has a name.
  2. Definition of a variable is the type of the variable.
  3. Instantiation of the variable is a piece of memory from RAM will be set aside for that variable.
  4. l-values are on the left side of the program and r-values are at the right side. l-values are the ones takes a piece of the memory like variables but r-values are the ones that don’t need to take memories and can be assigned to the l-values. Like numbers.
  5. It is a variable that has been defined but has no value yet assigned to it. An uninitialized variable may inherit some ‘garbage’ data previously assigned to the same memory address.
  6. Undefined behavior is the result of executing code whose behavior is not well defined by the language.
1 Like
  1. in C++ a variable is an object that is named.
  2. a named object that can store values of a pre-defined type such as an integer, float etc.
  3. the allocation of space for the variable.
  4. an lvalue has a permanent address. an rvalue does not.
  5. when defined the variable is not given an initial value and this leads to undefined code behaviour.
    6.behaviour that is not well defined by the language.
1 Like
  1. in C++, a variable is simply defined as an object that has a name
  2. a variable is a value that can change, depending on conditions or on information passed to the program
  3. C++ will let you both define a variable AND give it an initial value in the same step. This is called initialization
  4. An l-value is a value that has a persistent address (in memory). Since all variables have addresses, all variables are l-values
    e.g. int y = 4;
    an r-value is one that has a temporary existence, such it’s discarded after usage.
    e.g. x = 2 + 4, such that the 2 and 4 are r-values
  5. an uninitialized variable is one that has not been assigned a particular value
    e.g

#include

using namespace std;

int main() {

int x;

return x;

}

The return value would then be a random number value returned.

  1. Undefined behavior is the result of executing code whose behavior is not well defined by the language. The C++ language doesn’t have any rules determining what happens if you use value of a variable that has not been given a known value. Consequently, if you actually do this, undefined behavior will result.
1 Like
  1. An object that has a name.

  2. A declaration statement to create a variable.

  3. A piece of RAM that is set aside when the CPU executes a statement.

  4. l-value has a persistent address. r-value are not associated with a persistent memory address.

  5. A variable that hasn’t been given a known value. It can contain a random value or cause a warning.

  6. The result of executing code whose behavior is not well defined by the language.

2 Likes

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

What is Definition of a variable?
A special kind of declaration statement

What is Instantiation of a variable?
When a statement is executed by the CPU, a piece of memory from RAM will be set aside.

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).
An r-value refers to values that are not associated with a persistent memory address.

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, the default value of that variable is whatever (garbage) value happens to already be in that memory location.

What is undefined behaviour?
Undefined behaviour is the result of executing code whose behavior is not well defined by the language.

1 Like
  1. What is a variable in C++?
    Variable is an object that has a name

  2. What is Definition of a variable?
    Variable is assigned value

  3. What is Instantiation of a variable?
    Instantiation is a piece of memory from RAM that is set aside for the variable.

  4. What is the difference between an l-value and an r-value?
    L value will have persistent address in memory r value are in turn temporary in nature and do not have an address and are discarded in the end of the statement

  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 value is called an uninitialized variable. The default value of the variable will be whatever is stored in the memory location.

  6. What is undefined behaviour?
    Is the result of executing code whose behavior is not well defined by the language, example being uninitialized variable.

1 Like

1 An object that has a name

2 A kind of declaration statement.

3 When a statement is executed by the CPU and a piece of memory from RAM will be set aside.

4 l-value has a persistent address.
r-value refers to values that are not associated with a persistent memory address.

5 A variable that has not been given a known value through initialization or assignment. That is why we shouldn’t just initialize a variable. Like:

int x;

We should also assign it a value if we want to use it later on.

int x = 0;

Otherwise int x; can give us random numbers.

6 Such behavior happens when executing code whose behavior is not well defined by language. In other words, the code might produce random results.

2 Likes

What is Definition of a variable? : a special kind of declaration statement
What is Instantiation of a variable? : when a statement is executed by the CPU, a piece of memory from RAM will be set aside
What is the difference between an l-value and an r-value? : An l-value is a value on the left that has a persistent address, R-value are not associated with a persistent memory address
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) produces the default value of whatever (garbage) value happens to already be in that memory location
What is undefined behaviour? : result of executing code whose behavior is not well defined by the language

1 Like
  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?
    A declaration statement to create a variable

  3. What is Instantiation of a variable?
    statement is executed by the CPU, a piece of memory from RAM will be set aside.

  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). An r-value refers to values that are not associated with a persistent memory address.

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

  6. What is undefined behavior?
    Is the result of executing code whose behavior is not well defined by the language. In this case, the C++ language doesn’t have any rules determining what happens if you use value of a variable that has not been given a known value.

1 Like

What is a Variable in C++?

It is an object that has a name.

What is Definition of a Variable?

It is a declaration statement we use to create a variable.

What is Instantiation of a Variable?

When a statement is executed by the CPU, a piece of memory RAM will be set aside.

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

Variables are an l-type of value. An l-value is a value that has a persistent address (in memory).

r-values are opposite of l-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.

The key takeaway here is that on the left side of the assignment, you must have something that represents a memory address (such as a variable). Everything on the right side of the assignment will be evaluated to produce a value.

What is an Uninitialized Variable and What Kind of Behavior Can You Expect to See from Such a Variable?

A variable that has not been given a known value (through initialization or assignment) is called an uninitialized variable .

Uninitialized variables may produce incorrect, inconsistent results and could be a problem to debug when error messages occur.

Rule: Make sure all of your variables have known values (either through initialization or assignment). This ensures that your variable will always have a consistent value.

What is 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 incorrect result consistently.
  • Your program produces different results every time it is executed.
  • 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.
1 Like
  1. What is a variable in C++?
    an object as a piece of memory that’s used to store values then a variable is just an object with a name
  2. What is Definition of a variable?
    In order to create a variable, we use a special kind of declaration statement called a definition. For example: int x;
  3. What is Instantiation of a variable?
    assignment of a piece of memory from RAM to a variable
  4. What is the difference between an l-value and an r-value?
    l-value has an adress in memory where values can be stored, r-value do not have a persistent memory address
  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 behaviour?
    when the program is running your code with uninitialized variables and gives us unexpected behaviours. Because the variable is undefined if it is not assigned
1 Like

What is a variable in C++?

An Object with a name given to it.

What is Definition of a variable?

int y;

What is Instantiation of a variable?

A piece of memory from RAM set aside to hold the value of a variable.

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).

An r-value refers to values that are not associated with a persistent memory address.

int y = 5; y is the l-value and 5 is the r-value.

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

This is where a variable is not given a value.

It will hold whatever value was in the piece of memory where it sits.
example int y; is uninitialized.
int y=5; is initialized.

Using unitialized variables will give unknown behaviour when executing your code.

What is undefined behaviour?

Any of the following:

Your program produces an incorrect result consistently.
Your program produces different results every time it is executed.
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.

1 Like