Variables in C++ - Reading Assignment

1. What is a variable in C++?
A named region of memory (object), which can be used to store data.

2. What is the Definition of a variable?
The assignment of a name, and declaration of the type for a given variable.

3. What is Instantiation of a variable?
The allocation of a location in memory to the variable so it can begin to store data.

4. What is the difference between an l-value and an r-value?
Every expression is either lvalued or rvalued.
lvalue (locator value) is an object with an identifieable location in memory, an address which persists.
rvalue is the opposite, having no persistant address.

5. What is an uninitialized variable and what kind of behavior can you expect from such a variable?
It is a variable with no value assigned, as such the behavior can be unpredictable. Though some benefits may be offered, generally it is advised to initialize all variables.

6. What is undefined behavior?
As the name suggests, it is behaviour which is not defined(unclear, uncertain) usually as a result of executing code not fully defined (lacking value, memory allocation or identifiers)

1 Like
  1. A variable in C++ is a named object.
  2. Definition of variable is a special declaration statement used to create a variable. (ie. x or y).
  3. Instantiation of a variable is when a program is run an object is created and stored in a memory address.
  4. An lvalue defines a specific memory location, where as a rvalue does not define any location.
  5. An unutilized variable is a variable that has not been given a know location, causing you to get or not get the same results in your code.
  6. Undefined behavior is code that is not well defined in C++ code causing many potential issues to occur in your code.
1 Like

What is a variable in C++? | A named Object, with an Object being a region of storage that has a value and associated properties.

What is Definition of a variable? | A named Object.

What is Instantiation of a variable? | The Object is created and assigned a memory address.

What is the difference between an l-value and an R-value? | What?

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

What is undefined behavior? | What?

1 Like
  1. A variable is a named object within C++.
  2. The definition of a variable is an named object that when called upon obtains a portion of indirect memory that is used to run a program.
  3. It means that a variable has been not only given a name but also is assigned a memory address for information retrieval.
  4. I values always have a defined region of storage, r values do not and may not have any value attached to it.
  5. A variable that has not been given a known value; the outcome of the program may change or the variable could be printed or not printed for any given reason no matter how many attempts are made at running the program to expect a known outcome, it will mostly give an error.
  6. The result of executing code tht is not well interpreted by being undefined by the C++ language.
1 Like
  1. What is a variable in C++? - A named object
  2. What is the Definition of a variable? - Memory location for storing an object
  3. What is Instantiation of a variable? - Its when an object is created and assigned a memory address
  4. What is the difference between an l-value and an r-value? - 1. l-values are values that have their place in memory, r-values are values (or expressions) that are discarded at the end of a statement and therefore need not to be associated with a memory address
  5. What is an uninitialized variable and what kind of behavior can you expect from such a variable? It is a variable that has not been “started”, the compiler sends an error to fix it.
  6. What is undefined behavior? - It is executing code that not been well defined.
  1. What is a variable in C++? A named object given to a memory location***

  2. What is Definition of a variable? it makes a note to itself that we are defining a variable, giving it the name x, and that it is of type int***

  3. What is Instantiation of a variable? Object created and assigned to a memory address.***

  4. What is the difference between an l-value and an r-value? An l-value refers to memory location which identifies an object. An r-value refers to data value that is stored at some address in memory.***

  5. 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 been given a value by the program. Using the value stored in an uninitialized variable will result in undefined behavior.***

  6. What is undefined behaviour? Result of executing code whose behavior is not defined by the language specification***

1 Like
  1. A named object
  2. A Variable is a location of stored memory of a variable.
  3. The instantiation of a variable is the instance that the variable name and type is defined.
  4. An l-value is a reference object with a specific address in memory, a r-value is an object with no memory address.
  5. An uninitialized variable is a variable that has not been given a value at is instantiation.
  6. This behavior stems from the issue of using uninitialized variables
1 Like
  1. What is a variable in C++?
    A named object (in C++ an “object” does not include functions)

  2. What is the Definition of a variable?
    The statement which defines the name of the variable

  3. What is Instantiation of a variable?
    At runtime, the object associated with the variable is created and stored in a memory address

  4. What is the difference between an l-value and an r-value?
    L value has a specific memory location - it’s the container. R value is a temporary value, not stored in a memory address, the value assigned to a variable - it’s the data contained in the container.

  5. What is an uninitialized variable and what kind of behavior can you expect from such a variable?
    A variable which has not been given a value by the program. Using an uninitialized variable will result in undefined behavior.

  6. What is undefined behavior?
    The result of executing code whose behavior is not well defined by the C++ language

1 Like

[quote=“ivan, post:1, topic:3160”]

  1. What is a variable in C++? Value storing memory with a type name.
  2. What is the Definition of a variable?Is a coded expression storing numbers and names.
  3. What is Instantiation of a variable?Is the creation of an object with memory adress, real instance of computer process.
  4. What is the difference between an l-value and an r-value?I value has an assigned memory but not the r value.
  5. What is an uninitialized variable and what kind of behavior can you expect from such a variable?Is a variable with no value, with that we can get an undefined behavior.
  6. What is undefined behavior?
    It may run fine or crash unexpectedly
1 Like
  1. A named object.
  2. A named region of storage that has value and other associated properties.
  3. The creation of the named object and the assignment of its memory address.
  4. An l-value is a function or object. An r-value is evaluated for it’s value and cannot be assigned to.
  5. A declared variable that has not been initialised. This is one way to get undefined errors.
  6. Undefined behaviour causes the program to exhibit unexpected results.
1 Like
  • 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.

1 Like
  1. a named object
  2. a declaration statement used to create a variable
    3.a piece of RAM set aside for the variable
  3. An l value has an address within the memory an R value does not
  4. It is in the memory but doesnt have a value assigned to it and can cause unexpected behaviour
  5. Undefined behaviour is when the program exhibits unexpected results
1 Like

UUUUUUUUUUUummmmmmmm…
The last three questions were not talked about in the article if someone could explain to me what they are…

  1. Data storage that requires you to search through the object a get value
  2. A tool for storing data
  3. It means that they are created and given a memory address before use
    4,5,and 6 I have no idea what they are because they were not in the article
1 Like

Have you tried looking online? :wink: A large part of coding is searching online to get the answers.

Good idea. I’ll try that. :+1:

1. What is a variable in C++?
An named object in the memory.

2. What is the Definition of a variable?
A special kind of declaration statement.
eg.
int x; //define a variable named x, of type int

3. What is Instantiation of a variable?
Instantiation means creation of a named object with an assigned memory address.
The compiler will notice that we are defining a variable.
Finally, when the program is run (called runtime), the variable will be instantiated.

4. What is the difference between an l-value and r-value?
l-value is anything whose address is accessible. It means we can take address of r-value using & operator.
eg.
int x = 11; // x is an l-value because we can access the address of x i.e

r-value is anything that is not l-value. It means we can not take address of r-value and it also don’t persist beyond the single expression.
ie
int x = 1;
Here we can not take the address i.e

  int * ptr = &(1);    // Compile Error

Therefore ,1 is not a l-value and hence it 1 is r-value.

5. What is an uninitialized variable and what kind of behavior can you expect from such a variable?
An object that the Programmer has not given a known value.
If the Programmer stores a value in an uninitialized variable it will result in undefined behavior.

6.What is undefined behavior?
Undefined behavior is the result of executing code whose behavior is not well defined by the language. The result can be almost anything, including something that behaves correctly.

2 Likes
  1. Examples of a variable in C++ are int, and double.
  2. A variable is a name given to a memory location. It is the basic unit of storage in a program.
  3. When an object is created and assigned a memory slot
  4. An l-value refers to an object that persists beyond a single expression. An r-value is a temporary value that does not persist beyond the expression that uses it.
  5. Uninitialized means the object has not been given a known value (through any means, including assignment). Therefore, an object that is not initialized but is then assigned a value is no longer uninitialized (because it has been given a known value).
  6. Undefined behavior occurs when a program does something the result of which is not specified by the standard.
1 Like

1an object
2 the type of variable is defined by using one of the types in c++ e.g int =x
3 the spot the variable is held in the ram
4. -I- have a spot that can be brought from the memory for another use whereas r values don’t since they are 1 time use
5 declared variable whose value in the memory address has not yet been initialized. using such variables = undefined behaviour
6 He can work well the three first executions and not at the fourth for example.

1 Like
  1. What is a variable in C++?
    A named object
  2. What is Definition of a variable?
    A special kind of declaration statement, used in order to create a variable
  3. What is Instantiation of a variable?
    The object will be created and assigned a memory address.
  4. What is the difference between an l-value and an r-value?
    l-values are values that have their place in memory, r-values are values (or expressions) that are discarded at the end of a statement and therefore need not to be associated with a memory address
  5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
    A declared variable whose value in the memory address has not yet been initialized. using such variables can lead to undefined behaviour
  6. What is undefined behaviour?
    Undefined behavior is when the result of the program may exhibit unexpected results. He can work well the three first executions and not at the fourth for example
1 Like