9

The standard says

A variable is introduced by the declaration of an object. The variable's name denotes the object.

But what does this definition actually mean?

Does a variable give a name to an object, i.e. are variables just a naming mechanism for otherwise anonymous objects? Or is a variable the name itself?

Or is a variable a named object in the sense that every variable is also an object?

Or is a variable just a "proxy" with a name that "delegates" all operations to the real object?

To confuse things further, many C++ books seem to treat variables and objects as synonyms.

What is your take on this?


About entities, quoting from the C++0x draft:

An entity is a value, object, reference, function [...]

Every name that denotes an entity is introduced by a declaration.

A variable is introduced by the declaration of an object

From these statements I draw the conclusion that a variable is a name and thus cannot be an object. This is really confusing the hell out of me :)

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • My lame attempt: A variable is a lvalue that can be assigned to, if it is not marked `const`. – Mateen Ulhaq Dec 29 '19 at 03:16
  • A variable is essentially just a *label* providing access to the contents of a specific memory location -- as compared to a pointer that holds the address of a specific memory location as its value. – David C. Rankin Dec 29 '19 at 04:17

9 Answers9

18

Variables are named objects. The following create objects that are not variables

new int // create one int object
std::string() // create one string object

The following creates one array variable with name "foo" and 5 unnamed (sub-) objects of type "int"

int foo[5];

The following is not a variable in C++03, but has become a variable in C++0x (declared references are variables in C++0x, for details see the link)

extern int &r;

Does a variable give a name to an object, i.e. are variables just a naming mechanism for otherwise anonymous objects?

Variables are objects (or references respectively). The entity list (3/3 in C++03) of C++ contains multiple such is-a relationships. For instance, a sub-object is-a object and an array element is-a object and a class-member is-a object or function or type or template or enumerator.

The entity list of C++0x looks a bit cleaner to me, and it doesn't contain "variables", "instance of a function" (what that kind of entity even is has never been apparent to me), "sub-object" and "array element" anymore. Instead it added "template specialization" which either are functions, classes or templates (partial specializations).

The C++ object model at 1.8 says

An object can have a name (clause 3).

So if you like, you can formulate the statement as "The object's name denotes the object.".

q-l-p
  • 4,304
  • 3
  • 16
  • 36
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 1
    Only *declared* references are variables in C++0x ;) For example, `static_cast(person)` is not a variable, but an "anonymous reference" for lack of a better term. – fredoverflow May 27 '10 at 12:04
  • An more illustrative example of an object that isn't a variable is an anonymous object passed in as an argument to something else. – detly May 27 '10 at 12:07
  • @FredOverflow good point, i'll incorporate it into the answer after reading some FCD :) – Johannes Schaub - litb May 27 '10 at 12:20
  • Thanks for mentioning the entity list, I have updated my question. Please read the update and reply ;) – fredoverflow May 27 '10 at 12:20
  • @FredOverflow i don't come to the same conclusion. The text says that a name that denotes an object is introduced by a declaration. And it says that the declaration of an object introduces a variable. I can't conclude from that that a variable is a name. – Johannes Schaub - litb May 27 '10 at 12:35
  • Could you post the complete entity list of C++03? If that list clearly states that variables are objects, the case is solved. – fredoverflow May 27 '10 at 12:56
  • @FredOverflow the entity list does not state such relations directly. It just lists them. – Johannes Schaub - litb May 27 '10 at 12:59
  • Is there any other sentence that starts with "a variable is"? – fredoverflow May 27 '10 at 13:03
  • @FredOverflow: Is that a fallout of [our recent discussion of what's an object in C++](http://stackoverflow.com/questions/2910587/2910753#2910753)? – sbi May 27 '10 at 18:58
  • @sbi No, but thanks for reminding me to answer in that discussion :) – fredoverflow May 27 '10 at 19:09
2

Variables are names that you give to objects, so yes, objects are, by and large, anonymous.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • I'm finding it difficult to infer which part of your answer is inaccurate because of the downvote. Is it because the answer is a mere sentence which is not very technical? Or is it because a majority of objects are not anonymous? – Ardent Coder Nov 26 '20 at 05:49
2

Here's the definition from the C++17 standard:

A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name, if any, denotes the reference or object.

My take on this, quite frankly, is that that's not really a definition. It tells us what introduces a variable, but it doesn't define what a variable is.

Consider, for example:

int foo = 42;

This is a declaration of an object, so it "introduces" a variable. But what is the variable? Is the object named foo a variable? I would have thought so, but the definition doesn't actually say that. Apparently the "variable" has a name (in this case "foo") and that name denotes the object. Since it has a name, presumably the name itself is not the variable. And it would have been easy enough to say that the variable is the object, rather than that the variable's name denotes the object, if that were the intent.

What is a "variable" in C++? I really don't know, and I don't believe it's possible to answer the question based on the wording in the standard. (And I'd like that to be corrected in a future edition.)

(The C standard deals with this by not defining "variable" and, for the most part, not using it except as an adjective or informally.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • "*What is a "variable" in C++?*" It is a thing which denotes a reference or object. That thing may have a name. A variable is not an object/reference; it [denotes](https://www.dictionary.com/browse/denote) an object/reference. I don't know what more needs to be said. The standard doesn't define conceptually what an "entity" is either; it simply says that things X, Y and Z are entities. – Nicol Bolas Dec 29 '19 at 05:55
  • @NicolBolas That still only says what it does, not what it is. It's a "thing". What kind of thing? If I write "foo" on a piece of paper, it denotes that object. Is the paper a variable? How is "variable" a useful concept? – Keith Thompson Dec 29 '19 at 06:19
  • My point is that the standard doesn't answer questions like that in general. The standard doesn't explain what something means, it simply describes what it is. When a declaration introduces an object or reference, that process introduces a variable, whose name (if any) denotes the object or reference. – Nicol Bolas Dec 29 '19 at 14:23
  • @NicolBolas: And my point is that definitions should be definitions. After reading that, I still don't know what a "variable" is. I do know what an "object" is, and what an "identifier" is, because those terms have definitions that tell me what is and is not an object or an identifier. – Keith Thompson Dec 29 '19 at 20:48
  • "*I do know what an "object" is*" OK: What is an object? Because the C++ standard definition of that is equally murky: "An object is created by a definition (6.1), by a new-expression (8.3.4), when implicitly changing the active member of a union (12.3), or when a temporary object is created (7.4, 15.2). An object occupies a region of storage in its period of construction (15.7), throughout its lifetime (6.8), and in its period of destruction (15.7)." That says how it comes to be and where it lives, but not *what it is*. – Nicol Bolas Dec 29 '19 at 20:54
  • @NicolBolas: I hadn't realized until now that the C++ standard's definition of "object" has changed. In C++14 and earlier, it says "An *object* is a region of storage." followed by some statements about objects, including "An object is created by ...". C++17 drops the simple definition and put "An *object* is created by ..." in italics, making that the definition. And now an object **occupies** a region of storage rather than **being** a region of storage.So I used to know what an "object" is, but as of C++17 I don't. – Keith Thompson Dec 29 '19 at 23:18
  • But "is a region of storage" is actually incorrect. If an object is the storage it occupies, then destroying the object must destroy that storage. But that's not true; there are many ways to destroy an object while leaving its storage around. It is more accurate to say that an object exists within a region of storage. An object is no more its region of storage than you are your house. – Nicol Bolas Dec 29 '19 at 23:42
  • My original point is that the C++ standard's definition of "variable" doesn't define the word -- and it could and should have done so. Do you disagree? – Keith Thompson Dec 30 '19 at 00:19
  • And my point is that the standard defines the term as much as it needs to. A person might want some simplistic reduction of a concept to a phrase ("an object is a region of memory"), but a *standard* doesn't need any such thing. It defines how things behave. – Nicol Bolas Dec 30 '19 at 00:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205047/discussion-between-keith-thompson-and-nicol-bolas). – Keith Thompson Dec 30 '19 at 02:36
0

A variable is simply an entity with a type and a name

Chris
  • 1,432
  • 1
  • 10
  • 8
0

As I have learned

What are variables

A variable is an identifier that is bind to a value stored in the system's memory(imperative languages) or an expression that can be evaluated(functional languages).

Why we need variables

  • provide names for storage chunks to store data temporarily during the lifespan of a computer program.
  • are also used in programming to transfer information from one part of the program to another part(Eg: parameters, global variables).
TTDS
  • 114
  • 1
  • 10
-1

I think that this definition is quite clear.

The variable is introduced by declaration and denotes the object. Who introduces the variable? You do of course, and thus it is you who uses it.

A variable is really only a convenience for you the developer. It is a fundamental aspect of most programming languages not just C++. A variable mearly gives a symbolic name to a useable entity that occupies storage, such that it can be referenced and used at a future point in your source code.

For example if you declare a variable in a method as such:

int x = 5;

This will be reduced by the compiler to some offset from the stack pointer, say SP + 0x003.

At some point later you can say:

x = 52;

In this case the area of stack memory SP + 0x003 will contain the bytes that describe the number 52.

You declare the variable to be of a certain type so that the compiler can work out how much space the data occupies in memory. Without variables, you would have to manage all of the arrangement of information yourself and you would probably be coding in assembly or lower.

Adrian Regan
  • 2,240
  • 13
  • 11
  • 1
    I find your definition "The variable is introduced by declaration and denotes the object" very clear, but unfortunately, that's not what the standard says :) According to the standard, one declares *objects*, and it is not the variable that denotes the object, but the variable's *name* :( – fredoverflow May 27 '10 at 12:32
  • Hey, maybe I should even become a standards writer :-). As an aside, I think learning a language by reading it's standards document is a form of self harm... – Adrian Regan May 27 '10 at 13:18
-1

A variable is really a name given to an object in memory and hence an object is an anonymous type in that respect just at the point before compilation, when the compilation occurs, the variable is kept track of during the syntactical and parsing phase, then when the linker kicks in, that variable will have a memory address assigned to it, although at run-time, that memory address will be correctly off-setted somewhere to take into account of dynamic linking or static linking. Hence the variable can then be easily referenced aka the memory address that the variable is assigned to.

Really, in a nutshell, the variable is to help the programmer to work out the junction points of execution where that variable is referenced in terms of machine code.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
-1

What is your take on this?

Variable is a block of memory on stack or in code segment, or a value in a register (if the size of variable is small enough, although normally it is still value in a memory while registers hold temporary results), with name provided for programmer's convenience. Name of variable does not exist after compilation (we're not talking about macro tricks here). Any access to the variable is resolved into memory access, so technically variable is an address of corresponding data block, and that address isn't stored anywhere. Think about declaration of variables in assembly languages - variable "kinda" exists, but it is still merely an offset to the data block.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • Objects exist on the heap, on the stack, or in the data segment. – ChrisW May 27 '10 at 12:13
  • You understood me, so there is no reason for nitpicking. – SigTerm May 27 '10 at 12:17
  • The only good answer really. Variables are really just pointers to memory addresses which the compiler figures out for you because it's more idiomatic to use a name rather than a number. – Zorf May 27 '10 at 18:04
-1

Variable is a name for the object. You access the object through this named entity.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186