Variables in C++ - Reading Assignment

  1. What is a variable in C++?
    A named object is called a variable. A variable is a named object which serves as region of storage (usually memory) that has a value and other associated properties.

  2. What is Definition of a variable?
    In order to create a variable, we use a special kind of declaration statement called a definition wherein the object is assigned a name and type.

  3. What is Instantiation of a variable?
    When the program is run (called runtime), the variable will be instantiated. Instantiation is a fancy word that means the object will be created and assigned a memory address. Variables must be instantiated before they can be used to store values

  4. What is the difference between an l-value and an r-value?
    An l-value is a value which has an address. Thus all variables are l-values since variables have addresses. Since the name l-value stands for left-value, so these are always on the left side in an assignment statement (a statement in which we assign the value from the right side to the left side). Therefore whenever a variable is assigned some value, it is always written on the left side of the assignment operator (=).

In C++, the value on the right side of = is assigned to the left side. E.g.- x = 8 will make x equal to 8 but 8 = x will give you error because you can’t assign any value to 8, which is not a variable.

An r-value is a value which is assigned to an l-value. For example, in the statement x = 8; , x is a l-value since it is a variable to which some value is being assigned and 8 is an r-value since it is being assigned to x. Note that when we write 5 = 8; , there will be a compilation error because 5 is not an l-value (since it is not a variable having any memory space).

  1. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
    An uninitialized variable is the one to which we have not assigned any value. Unlike some programming languages where a variable which has not been assigned any value takes a value zero, an uninitialized variable in C++ by default takes some garbage value.

  2. What is undefined behaviour?

  • undefined behavior - there are no restrictions on the behavior of the program. Examples of undefined behavior are memory accesses outside of array bounds, signed integer overflow, null pointer dereference, [more than one] modifications of the same scalar in an expression without any intermediate sequence point (until C++11) that are unsequenced (since C++11), access to an object through [a pointer of a different type etc. Compilers are not required to diagnose undefined behavior (although many simple situations are diagnosed), and the compiled program is not required to do anything meaningful.

** hackers use this in buffer overflow attacks to take over computers ; }

1 Like
  • A variable is a named object.
  • An element, feature, or factor that is liable to vary or change.
  • Instantiation is a fancy word that means the object will be created and assigned a memory address.
  • An l-value is persistently defined in memory. An r-value is not defined persistently and may be considered the variation of the l-value variable.
  • A variable that has no memory location. We can expect anything except what we expected the variable value to be.
  • The resulting value of an uninitialized variable.
1 Like

I cannot access to the article.
Screen Shot 2021-04-04 at 20.01.36

Could this be an alternative?
https://www.learn-cpp.org/en/Variables_and_Types

  • What is a variable in C++?
    In C++, direct memory access is not allowed. Instead, we access memory indirectly through an object. An object is a region of storage (usually memory) that has a value and other associated properties. Objects can be named or unnamed (anonymous). A named object is called a variable, and the name of the object is called an identifier. In our programs, most of the objects we create will be variables.
  • What is the Definition of a variable?

A named object is called a variable.

  • What is Instantiation of a variable?

When the program is run (called runtime), the variable will be instantiated. Instantiation is a fancy word that means the object will be created and assigned a memory address. Variables must be instantiated before they can be used to store values.

  • What is the difference between an l-value and an r-value?
    In C++ an lvalue is something that points to a specific memory location. On the other hand, a rvalue is something that doesn’t point anywhere.
  • What is an uninitialized variable and what kind of behavior can you expect from such a variable?

The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea. It has to be initialized with a value before you can use it

  • What is undefined behavior?

The variable is undefined so it has no idea what it is supposed to do. Therefore, undefined behavior is doing something different by the variable every time. I think this could cause programming issues and possibly bugs.

1 Like

What is a variable in C++?

It is a named object, also known as an identifier

What is the Definition of a variable?

A variable is an identifier that refers to the data item stored at a particular memory location.

What is Instantiation of a variable?

Instantiation is a fancy word that means the object will be created and assigned a memory address. Variables must be instantiated before they can be used to store values.

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

l-value refers to memory location which identifies an object. L-value may appear as either left hand or right hand side of an assignment operator (=). L-value often represents as identifier.
R-value refers to data value that is stored at some address in memory. An r-value is an expression that can't have a value assigned to it which means r-value can appear on right but not on left hand side of an assignment operator (=).

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

An uninitialized variable is an undefined variable. This creates an undefined behaviour when executing the code.

What is undefined behavior?

It allows compilers to avoid lots of checks. Also, when a program does something the result of which is not specified by the standard.

2 Likes
  1. What is a variable in C++?
    A= a variable is a named object
  2. What is the Definition of a variable?
    A= they are values stored on the PC memory
  3. What is Instantiation of a variable?
    A= It means that an object will be created an assigned a memory address, and variables must be instantiated before they can be used to store values.
  4. What is the difference between an l-value and an r-value?
    A= The C++17 standard defines expression value categories as follows:
  • A glvalue is an expression whose evaluation determines the identity of an object, bit-field, or function.
  • A prvalue is an expression whose evaluation initializes an object or a bit-field, or computes the value of the operand of an operator, as specified by the context in which it appears.
  • An xvalue is a glvalue that denotes an object or bit-field whose resources can be reused (usually because it is near the end of its lifetime). Example: Certain kinds of expressions involving rvalue references (8.3.2) yield xvalues, such as a call to a function whose return type is an rvalue reference or a cast to an rvalue reference type.
  • An lvalue is a glvalue that is not an xvalue.
  • An rvalue is a prvalue or an xvalue.
  1. What is an uninitialized variable and what kind of behavior can you expect from such a variable?
    A= The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea. It has to be initialized with a value before you can use it.
  2. What is undefined behavior?
    A= It means that there are no restrictions on the behavior of the program
    Because correct C++ programs are free of undefined behavior, compilers may produce unexpected results when a program that actually has UB is compiled with optimization enabled
1 Like

Hmm, seems to be working for me now. Maybe it was just down for a short while. You can also use the other article. There are in fact a lot of resources like it online :slight_smile:

  1. a variable is a named object assigned to memory
    2.defining a variable gives it a name and a type
  2. object will be created and assigned a memory adress, sometimes called an instance
    4.I dont know
    5.and uninitialized variable hasnt been run yet
  3. ?
1 Like
  1. A named object
  2. Data structure in memory, or function.
  3. The object will be created and assigned a memory address.
  4. l-value is a variable with an address and r-value is a variable without an address.
  5. Uninitialized variable is a variable that has not been given a known value (usually through initialization or assignment). Using the values of uninitialized variables can lead to unexpected results.
  6. Undefined behavior 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.
1 Like

It basically refers to the left and right side of the assignment operator.

Its behavior that is not well defined in the language specification. :slight_smile:

  1. A variable is a named object
  2. the memory location holding that object
  3. its created and assigned to a memory adress
  4. R Value is the expression f the value to be assigned,; I value is the value assigned
  5. Its declared but not instantiated and unpredictable
  6. could be inconsistent and error prone
1 Like
  1. Variables are numbers, strings, and objects.

  2. The way you define a variable is, type and name.

  3. Instantiation means that object will be created and assign a memory address.

  4. l-value refers to memory location which identifies an object. It can be written in either left or right side of = sign. R-value refers refers to data vale that is stored at some address in memory. It can only be written at the right side. In simpler terms, l-value have a name, r-value doesn’t have a name.
    Example:

    int a; //l-value
    int n = 5; //l-value
    makeNoise(); //r-value, its life is very short
  1. Uninitialized variables are variables that are only defined but have no values assigned. If you try to print it out, you get random numbers (generated by a complier).

  2. Undefined behavior is when the program does something the result of which is not specified by the standard. For example, diving by 0.

1 Like

Yes it is working now!

  1. A named object is called a variable in C++.

  2. A definition is a is a special kind of declaration statement.

3 .Instantiation is when an object is created and assigned an address in memory. Variables must ne instantiated before they can be used to store a value.

  1. An l-value also called a locator value has been assigned a space in memory and can be called again. An r-value is a temporary values that can be the result of an expresion and are not assigned a place in memory.

  2. An uninitialized variable is a variable that has not been given a value by the program and can be different every time the program is run. Using the value stored in an uninitialized variable will result in undefined behavior

  3. Undefined behavior is the result of running code that’s result is unpredicatable and can change every time it is run.

1 Like
  1. A variable in c++ is defined as a named object.

  2. A declaration statement.

  3. Instantiation of a variable is a kind of storage of memory which is set aside for when the defined variable is created.

  4. An I-value has a persistent address and an R-value does not.

  5. It is a variable which hasn’t yet been assigned memory.

  6. It is as a result of using code in which the behaviour isn’t defined by a specific language.

1 Like
  1. A variable is a named object, with the corresponding name being the identifier.
  2. A declaration statement used to create the variable.
  3. The creation of a variable followed by an assignment to a memory address.
  4. l-values are left side variables that have a persistent address in the RAM. r-values are are right side variables that have no such address and become discarded after their use.
  5. Value has not been assigned to such a variable and will result in unpredictable behavior.
  6. A variable must be initialized; if not, the behavior is undefined.
2 Likes
  1. What is a variable in C++?
    an object with a name

  2. What is Definition of a variable?
    a declaration giving the datatype and the variable name.

  3. What is Instantiation of a variable?
    An instantiation is a piece of memory set aside by the RAM. An instantiation is executed by the CPU after defining a variable.

  4. What is the difference between an l-value and an r-value?
    l values are on the left side of the assignment symbol and have a persistent address in memory. r values are on the right side of an assignment symbol and do not have a persistent address in momery. they are temporary.

  5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
    variables where no specific value has been assigned to. such variables hold random values so you can also expect random behaviour

  6. What is undefined behaviour?
    executing code whose behavoir is not well defined by the language for example using an uninitialized variable.

2 Likes
  1. an object that has a name.

  2. variable iss a named storage that holds some value for our program to manipulate

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

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

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.

  1. An uninitialised variable is a variable that has not been given a known value.

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

2 Likes

1. What is a variable in C++?

  • A named object is called a variable, and the name of the object is called an identifier. In our programs, most of the objects we create will be variables.

2. What is the Definition of a variable?

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

ie: Define a variable named x:

int x; // define a variable named x, of type int

3. What is Instantiation of a variable?

  • When the program is run (called runtime), the variable will be instantiated.

  • Instantiation is a fancy word that means the object will be created and assigned a memory address.

  • Variables must be instantiated before they can be used to store values.

  • An instantiated object is sometimes also called an instance.

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

  • l-values and r-values are fundamental to C++. l-value and r-value refer to the left and right side of the assignment operator

  • An l-value is an expression that refers to such an object. The term l-value originally referred to objects that appear on the left (hence the ’ l ') hand side of an expression

  • An r-value is any expression that has a value, but cannot have a value assigned to it. It’s an expression that does not represent an object occupying some identifiable location in memory.

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

  • An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.

6. What is undefined behavior?

  • Renders the entire program meaningless if certain rules of the language are violated.

  • Undefined behavior occurs when a program does something the result of which is not specified by the standard.

2 Likes

Q: What is a variable in C++?

A: A named object is called a variable. Objects are used to have indirect access to memory in C++.

Q: What is Definition of a variable?

A: To create a variable we have to define it in C++:

int x; // define a variable named x, of type int

Q: What is Instantiation of a variable?

A: A variable is instantiated when the program runs. It means that the object is being created and assigned to a memory address.

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

A: r-value has no specific memory address, just a temporary register while the program is running. l-value has a specific memory location.

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

A: It is a variable that it is not given a specific value. They tend to hold random values and tend to have random behaviour like undefined behaviour.

Q: What is undefined behaviour?

A: Result you get when executing code whose behaviour is not well defined.

1 Like