8

When I declare a variable, such as:

 int x = 6;

What exactly is x? an address in the memory is usually hexadecimal..
Also, when I'm calling X

x = 2;

How does the compiler know where x is? x isn't an address.

That was my first question.

Second:
Let's say i have an object:
Person p;
p has 2 datamembers:

 int type1, int type2;

What exactly is p, and why do I need to go to p, then the variable?

p.type1, p->type1.
Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • It gets pushed onto the stack. – Matthew Beck Oct 12 '12 at 14:09
  • 11
    I think you should pick up a [C++ book](http://stackoverflow.com/questions/194812/list-of-freely-available-programming-books). These concepts are pretty basic and are usually explained in the beginning. – default Oct 12 '12 at 14:11
  • 2
    I have some books.. each books only says "x" is a variable which can store data. I'm about to finish studying OOP, i'm not a beginner.. this question just appeared in my mind. S: – user1725859 Oct 12 '12 at 14:21
  • 5
    Programming language books will not tell you much about this. You should read a book on compiler construction. That's where you will learn all about l- and r-values the others are babbling about here. A good book is the so-called dragon book: http://en.wikipedia.org/wiki/Principles_of_Compiler_Design – Zane Oct 12 '12 at 14:48
  • Thanks for the book! i'll buy it. is it for beginners? i'm not an expert programmer XD – user1725859 Oct 12 '12 at 15:20
  • 4
    I'm unexplicably compelled to highlight the humor in your responses: *"I'm about to finish studying OOP, I'm not a beginner"* vs *"Is it for beginners? I'm not an expert programmer"* – Justin ᚅᚔᚈᚄᚒᚔ Oct 12 '12 at 19:04

11 Answers11

5
int x=6 ;

Here x is identifier i.e. the name given to memory area where value 6 is stored.
x is simple name just to identify memory area where value 6 is stored.
when u declares variable, that time compiler stores ur variable name in the identifier table.

For person p, here once again p is name given to the memory area,which stores two data member type1 & type2

For accessing the value of type1 & type2, first u have to find the memory area, where they are stored. That's why u have to first access the memory area p & then u can access type1 * type2

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
5

In the case int x = 6, x is just a name to help you write the code and the compiler compile it. It's basically an alias for some place in memory, so that it's easier to access it later via x = 2 - this tells both you and the compiler that you want to write the value 2 in that same place.

Same as before, but it takes up more space (sizeof(Person) to be exact). p->type1 is only valid if p is a pointer to a Person (or if you overloaded the -> operator, but it's not the case), p.type1 is the syntax used for an object, to specify which part of the it you want to access.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
4

x is an lvalue, which means "locator value". It's called like that because from x the compiler knows where it is located. So x in a sense is an address, or something that allows an address to be deduced.

That thing becomes a value only when read

int a = x;

In that case, the compiler takes x, reads the value in its location and henceforth in that initialization the x stands for its value. Unfortunately this "value reading" is an implicit process, so whether the text x is a value or a lvalue depends on where it appears in.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • *lvalue* means "locator value" now? – John Dibling Oct 12 '12 at 14:12
  • @JohnDibling "now"? Ever since C, it had that meaning. – Johannes Schaub - litb Oct 12 '12 at 14:13
  • 2
    Well, whaddya know. Thought it meant something else. – John Dibling Oct 12 '12 at 14:14
  • 1
    @JohnDibling I thought the 'l' stood for left - as in it can appear at the left of an assignment, as opposed to an r-value, which can only appear at the right... guess you learn something new every day. – Luchian Grigore Oct 12 '12 at 14:25
  • 2
    well when I was young it meant "left-value". And even Wikipedia has been fooled to believe this (http://en.wikipedia.org/wiki/Value_(computer_science)). But if l-value is locator value, what exactly was the meaning of r-value? – Zane Oct 12 '12 at 14:42
  • 1
    "we shall give these two values the neutral names: L-value for the address-like object appropriate on the left of an assignment, and R-value for the contents-like object appropriate for the right." \[[fundamental-1967](http://www.itu.dk/courses/BPRD/E2009/fundamental-1967.pdf)] – Xeo Oct 12 '12 at 14:52
  • 1
    @litb: If *lvalue* means "locator value", what does *rvalue* mean? – John Dibling Oct 12 '12 at 14:54
  • 1
    The earliest use of `lvalue` of which I am aware was in K&R. I have the 1st edition K&R (1978) here ... `The name "lvalue" comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression`. – dajames Oct 12 '12 at 15:26
  • what i meant to refer to was lvalue in an iso c++ and c sense. the c standard say that it can be thouht of as meaning locator value. – Johannes Schaub - litb Oct 12 '12 at 16:15
  • @john the meaning of the l character in lvalue is not normatively defined. same for the r in rvalue. – Johannes Schaub - litb Oct 12 '12 at 16:17
3
int x = 6;

what exactly is X?

x is a variable which can be defined as a name given to a location in memory.

how does the compiler know where's x?

There is something called a symbol table which the compiler uses internally to map variable names to actual memory locations.

Raj
  • 4,342
  • 9
  • 40
  • 45
1

x is a variable identifier. A variable differs from an address in that it has a size, and can't be null. The address of x can be obtained with &x. Consider this code:

int x = 5;
int* p = &x; // the pointer p now refers to x
x = 6;

Now, even though you've only changed the value of x, if you get the value of *p (called 'dereferencing' the pointer) you will also get 6. p is also a variable, but it has the type 'pointer to int' rather than 'int', which is the type x has.

benwad
  • 6,414
  • 10
  • 59
  • 93
1

x is a variable. Or more exactly the name or label of the place where is stored that piece of information (data) that variable refers to. You need labels to differentiate between different variables. It's just there for you (the programmer) to help you.

p is again variable but of type Person. And Person is data structure - it holds data, that you access through the structure. And p is object (e.i. is instantiation of the structure) of this type. You can have two objects of the same type

Person p;
Person someone;

In order to know which one's data/member you want access you use p.data someone.data

Why p->data?

p->data syntax is equivalent to (*p).data

That goes to explaining what is pointer. As I said variable is a place where you store some information (data). Simply said pointer is a variable that stores a memory address. In other words it points to another value stored somewhere in the memory

For example:

Person p; // variable of type Person 
Person *pointer_to_p = &p; 
// a pointer that points to the variable p 
// it holds the address of p - that's what &p does it returns the address of a varible

*p in (*p).data is called dereferencing a pointer (accessing the value that the pointer points to)

For more information it is a good idea to google pointers or even better get a book to read

Community
  • 1
  • 1
tozka
  • 3,211
  • 19
  • 23
1

That it's a variable is all that can be said with confidence.

It might not have a memory address, if the optimizer placed it into a register. If it does have a memory address, it could be given a fixed absolute address (for globals and statics in non-relocatable code), an address relative to some other object (for non-static members), or an address relative to the stack frame (for automatic local variables).

It might not even be a storage location, if the compiler determined that it could always predict the value and substitute that value at every use.

If you use the & address-of operator on the variable, then the compiler will have to give it a memory location.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

The Word is not the Thing.

All x, and p, and for that matter p.type1 are is names, or identifiers. So, I guess you're really asking what they identify. The answer is that they identify something, which behaves as if it fulfills certain requirements made by the language specification. What this identified thing actually is will depend on your platform, compiler, maybe what optimizations you have turned on, etc. etc.

So, let's take the statement

int x = 6;

You're saying that x is now (in this scope and possibly nested scopes) going to refer to an int (whatever that is), and giving it the initial integer literal value 6.

So, what is an int? It's a thing which can hold a single (positive or negative) integer value, within the range [std::numeric_limits<int>::min(),std::numeric_limits<int>::max()]. Its exact size is architecture-dependent, but it's at least as big as a char and no larger than a long (IIRC).

Likely candidates for what this thing could be:

  • sufficient memory to hold whatever an int is on your platform, associated with a linker symbol (possibly relocated at runtime) if it's a global declaration, so all the code referring to x uses the same memory location
  • sufficient memory at a fixed offset from the stack (or stack-frame) pointer register if it's a function-scope local variable: the compiler is responsible for tracking the variables it's going to put here inside each function, and remembering which offset is associated with each identifier. This is a common default (unoptimized) case
  • a register, of sufficient size to store an int: if you never take the address of x it may be able to spend its whole life inside a register, but if you either take the address or the compiler runs out of registers for all your variables (or needs to save them across a function call), x may be loaded into a register when you're working with it, and spilled back to stack. Again, it's up to the compiler to remember when x is currently a register (and which one), and when it's a memory location

As you can see, the "thing" x identifies can really be lots of things, at different points during the execution of your program.

Unless you're optimizing something or carefully laying out your memory, a more useful question is what are the properties of x?

The point of the standard (or any language spec, really) is to make some useful guarantees about types, variables and behaviours, and then let the compiler choose how to achieve those guarantees.

Useless
  • 64,155
  • 6
  • 88
  • 132
1

Let's take a step back ... it's important to understand that all programming languages (apart from assembly language) describe an abstraction of a problem in terms that are supposed to be easy for programmers to think about. It is the job of the compiler to read that abstract description and to generate a lower-level description that corresponds directly to the hardware.

When you write:

int x = 6;

you are telling the compiler that you want to use an integer variable, that you want to call that variable x, and that you want the variable to have the value 6.

One of the jobs of the compiler is to decide how to store that variable. The C++ language describes various kinds of variable scope that help the compiler decide what sort of storage is appropriate.

  • A local variable (declared inside a function) will normally be stored in memory on the stack, but a small value (such as an integer) could be held in a register.

  • A global or static variable will be stored in memory.

The compiler remembers where it has chosen to store the value so that it can find it again -- for a local varaible that will be the register name or an address relative to the top of the stack; for a global or static it will be an address relative to the start of all the program's data.

The actual address in memory isn't known until the program has been compiled, linked, and loaded into memory (because the OS may not always load the program at the same address) -- the important thing is that the compiler knows where the variable will be, and that it can generate code to access it.

If, as in your second question, the type of the variable is some data structure the compiler chooses where to place it in memory in exactly the same way. When your program accesses a member of that structure the compiler can work out the address of the member because it knows the address of the structure and also the offset of the member within the structure.

So, in the case of your person p example, when the program refers to p1.type2 the compiler takes the address of p and adds the offset of type2 (which will probably be 4, because the first part of your struct person is taken up with type1 which is an integer, which is 4 bytes (on most 32-bit architectures)).

You have to specify both p and type2 because you might have another person (say q) and the compiler needs to be told which person you are trying to manipulate. p.type2 is not the same variable as q.type2, and will have a different address.

dajames
  • 2,776
  • 1
  • 20
  • 17
0

read about stack and heaps.

x will be pushed onto the stack with a unique adress. But you only have to call x.

but try cout << &x;

That is the real adress

Pavenhimself
  • 527
  • 1
  • 5
  • 18
-1

when you declare your variable, your compiler maps it with a variable attribute table. And that table contains address and size, type etc.So compiler gets things from this table when needed.

Avaneesh Kumar
  • 143
  • 1
  • 7