fredoverflow
fredoverflow

Reputation: 263340

What exactly is a variable in C++?

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

Upvotes: 9

Views: 952

Answers (9)

Keith Thompson
Keith Thompson

Reputation: 263617

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

Upvotes: 2

TTDS
TTDS

Reputation: 104

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

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507313

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

Upvotes: 18

Chris
Chris

Reputation: 1472

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

Upvotes: 0

fastcodejava
fastcodejava

Reputation: 41117

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

Upvotes: -1

Adrian Regan
Adrian Regan

Reputation: 2250

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.

Upvotes: -1

SigTerm
SigTerm

Reputation: 26439

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.

Upvotes: -1

t0mm13b
t0mm13b

Reputation: 34592

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.

Upvotes: -1

Marcelo Cantos
Marcelo Cantos

Reputation: 186078

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

Upvotes: 2

Related Questions