Variables in C++ - Reading Assignment

  1. It’s a named object that is kept in the computer’s memory.
  2. Is a portion of memory that stores the current name assignment.
  3. Instantiation occurs at compiling time, and creates an object and assign it to an address in memory.
  4. An l-value represents an identifiable memory locations (value assigned it), whereas an r-value are temporary ( eg: x = 2+2 -> x is l-value and 2+2 is r-value)
  5. Is a variable which has not been assigned yet, thus during running time will cause unexpected behavior.
  6. Undefined behavior it might be errors, crash, wrong results, etc.
1 Like

1.A named object is called variable.be used to store
2. Is a named object is called variable. Objects can be named or unnamed.
3. Is a fancy word that means the object will be created and assigned a memory address . Variables must be instatioled before they can be used to store values.
4. I-value is the assignee
r-value is a expression that determines the value that assign.
5. Is a variable is in the memory . By using that , it will cause unexpected behavior.
6.Undefined behavior is the result of a program may show unespected results.

1 Like

I have and have a better understanding, Thanks

1 Like
  • A named object
  • Memory location to hold value for the object
  • Reserving the memory slot for the object
  • I-value is assigned to the memory and more persistent, r-value is temporary and discarded after statement has been made
  • Its a variable that has not been given a value. If its used there can be anything assigned to that spot at the time.
  • For example what would happend if you use uninitialized variables, you dont know what u get, because c++ has no rules what would happen.
1 Like

1.What is a variable in C++?
A variable is a named region of memory
2.What is Definition of a variable?
variable is a location in computer memory where a value can be stored.
3.What is Instantiation of a variable?
To instantiate is to create an instance of an object in an object-oriented programming (OOP) language
4.What is the difference between an l-value and an r-value?
An lvalue is an object reference and an rvalue is a value
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?
undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres.

1 Like
  1. In C++ a variable is a named object.
  2. The definition of a variable is a special kind of declaration statement that is used to create a variable.
  3. Instantiation of a variable is when a variable will be created and assigned a memory address.
  4. An l-value (locator value) represents an object that has a location in memory (an address). An r-value is an expression that doesn’t represent an object with some sort of identifiable memory location.
  5. An uninitialized variable is a variable that has not been given a known value. If an uninitialized variable is used, then the computer might assign some unused memory to it, creating different results each time the program is run.
  6. Undefined behavior is the result of executing code whose behavior is not well defined by the C++ language. The language doesn’t have rules to determine what happens in these cases, so the results are unpredictable.
1 Like

A variable is an instantiated object that points to a memory location. Variables have different types (int, double, etc.)

In order to create a variable, we use a special kind of declaration statement called a definition, (int x; is a variable definition statement).

Instantiation is a fancy word that means the object will be created and assigned a memory address. This is done by the compiler when the program is run. Variables must be instantiated before they can be used to store values.

Um… Questions 4, 5 and 6 were not covered in this reading assignment, but I noticed that people are answering it nevertheless!! What am I missing here?!? This was the link I followed: Read this article (http://www.learncpp.com/cpp-tutorial/13-a-first-look-at-variables-initialization-and-assignment/). Nothing shown with regards to q’s 4, 5 and 6. Please assist!

1 Like

Hi @jonsax :slight_smile:
I am not sure how others came to the right answers, either the article was changed or they googled the answers themselves, I didn’t read it to be honest :sweat_smile:
To help you answer the questions:
4. l and r value refer to values on the left and right side of the assignment (x = 1).
5. and 6. If the variable was not assigned a value it can cause strange behavior in the code. :wink:

1 Like

Yeah, its strange, it’s not providing the info needed to answer the quiz! Thanks for confirming that!

1 Like

Programing in general requires a lot of research on your own to be able to solve the problems at hand :slight_smile: you can challenge yourself and try figuring these answers yourself :slight_smile:

1 Like

of course, I know that… But you’d think the quiz at the end of an assigned reading would ask questions that were covered in the reading, making me think “Am I missing something here?”. My real issue is not answering the quiz correctly but missing valuable reading material that I might not have seen! Sure I can google the answers, but my question is a valid one I think!

1 Like

Agree it is valid. I will talk to the team about it and see what they have to say :slight_smile:

1 Like

sweet… Please let me know! Will satisfy my curiosity…

  1. A variable is a named region of memory.
  2. A named object is called a variable.
  3. Instantiation means the object will be created and assigned a memory address.
  4. They are assigned to two different memory address.
  5. Variables have to be initialized otherwise we will get an error message.
  6. Everything has to be defined, otherwise the program will not work.
1 Like
  • What is a variable in C++?

A named object is called a variable. In C++ the term object has a narrower definition that excludes functions.

  • What is the Definition of a variable?

A special kind of declaration statement.

  • What is Instantiation of a variable?

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

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

L-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 : r-value” refers to data value that is stored at some address in memory. A 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?

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 (usually through initialization or assignment) is called an uninitialized variable.

Uninitialized variables can lead to unexpected results.

  • What is undefined behavior?

Using the value from an uninitialized variable is undefined behaviour. Undefined behaviour is the result of executing code whose behaviour is not well defined by the C++ language.

Code implementing undefined behaviour 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 it’s 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++?

A variable is a named object residing in a region of the computer’s memory.

2. What is the Definition of a variable?

The definition of a variable is a special kind of declaration statement, which the compiler processes and uses to give it the variable a name and a data type

3. What is Instantiation of a variable?

The instantiation of a variable is when (at runtime) the compiler creates the object and assigns it 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?

The difference between an l-value and an r-value is that an l-value has an address that your program can access; while an r-value does not.

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 assigned an initial value. Therefore, until it is assigned a value, it will contain only “junk” data, i.e., whatever happens to be in the memory address assigned to it.

6. What is undefined behaviour?

Undefined behaviour occurs when a program executes code having results not well defined by the language. Some examples would be memory accesses outside of array bounds, signed integer overflow, or a null pointer dereference.

1 Like

1. What is a variable in C++?
A variable is a storage of data with a name and a location in the memory.

2. What is the Definition of a variable?
Its delaration statement.

3. What is Instantiation of a variable?
Creating the variable (giving it name and type) and allocating it to some memory space.

4. What is the difference between an l-value and an r-value?
l-values point to a specific memory location while r-values don’t point anywhere.

5. What is an uninitialized variable and what kind of behaviour can you expect from such a variable?
This is a variable that has not been assigned a value. Such a variable will have whatever random value happens to be in the memory slot it was assigned to as a default value.

6. What is undefined behaviour?
It’s when you use uninitialised variables that produce weird, incoherent, inconsistent, unreliable results.

1 Like
  1. A variable in C++ is a named object. The name it’s given is called an identifier, while an object is a region of storage/memory that has various properties.

  2. A variable in general terms is a type of object. In C++, however, the term ‘object’ has a stricter definition: a variable, a data structure, but not a function.

  3. The instantiation of a variable is the creation and assignment of an object to a memory address.

  4. An l-value (L-value) is the location of an object/value in memory.
    An r-value is the contents of an object/value in memory.

  5. An uninitialized variable is a variable that has not been given an initial value. They are a primary source of bugs and errors for many programmers.

  6. Undefined behaviour is, as implied, code that is not defined according to the programming language’s specifications. For example, dividing by zero. This can have serious consequences, because the complier does not see the code as a problem, and can interpret the undefined behaviour as it wishes. It is usually unpredictable* in terms of output, and is another well-known source of bugs.

1 Like
  1. What is a variable in C++? A variable cannot have a function in C++

  2. What is Definition of a variable? A variable is a named object, or region of the RAM. The identifier is the name

  3. What is Instantiation of a variable? Create an object and assign a memory address

  4. What is the difference between an l-value and an r-value? L value = object that occupies an identifiable location in memory. Memory not registry. Identifiable address R value = anything that is not an L value.

  5. What is an uninitialized variable and what kind of behavior can you expect from such a variable? Uninitialized means that it hasn’t been assigned a value. It may have a value from an earlier program and give a wrong answer.

  6. What is undefined behavior? Executing code whose behavior is not defined in C++. If you do something in the program that has undefined behavior, there is no telling what will happen. If your code changes when you turn on the optimizer, it is undefined behavior.

Other vocabulary:

Data - Any information that can be moved, processed, or stored by a computer.

Value -single piece of data stored in a mailbox. Can be accessed by region or object

What is Definition of a variable? A variable is a named object, or region of the RAM.

The identifier is the name

Data type or type tells the compiler what type of information is stored, number, letter etc…

Integer -intiger refers to numbers, and C++ supports integers out of the box.

Programs are collections of instructions that manipulate data to produce a desired result.

Object -region of storage

1 Like
  1. A named object.
  2. It is a special kind of declaration statement.
  3. Creating and assigning a variable object to a memory address.
  4. l-value points to a specific memory location, an r-value does not point anywhere and is mostly short lived.
  5. A variable with an undefined value, using an undefined variable will result in undefined behavior.
  6. Undefined behavior is the result of executing code whose behavior is not well defined.
1 Like