views:

591

answers:

9

How does a variable in C/C++ work?

I mean, a pointer stores an address from a variable and then you have to dereference it to access the object to which it refers, so I think that a variable is a pointer that is dereferenced automatically when used... does that make any sense?

BTW, I didn't find an answer to this on google...

Thank you

+27  A: 

A variable is an abstraction (a convenient name) for a memory position on the computer. In C/C++ if the variable is of type int it will be a convenient name for a memory address holding an integer value.

And a variable is not a pointer automatically dereferenced. A variable just holds the value it is supposed to hold. If it is a pointer, it will hold a memory address, if it is an integer it will hold an integer value, if it is a float, it will hold a floating point number... And so on and so forth...

Pablo Santa Cruz
variables may not always exsist as an offset in memory, such as local vars, stored exclusively in registers. other than that, +1 for short and simple :)
Necrolis
True. Thanks for pointing that out. Although, you can consider **registers** as a type of memory. Can't you?
Pablo Santa Cruz
@Pablo Possibly, but you can't make a pointer to a register
Tim Robinson
@Tim: Didn't think of that. Great!
Pablo Santa Cruz
thank you all for your answers :)
Hugo
A: 

A variable actually does not do any work. Instead, a program may work upon variables.

Chubsdad
I think you misunderstood his question. He meant to ask how variables are implemented.
jalf
+9  A: 

Consider the following definitions

char *string="abc";
int b = 10;
int *bptr = &b;

I simplify this a little bit and use decimal values, the variables (names) are placeholder for adresses, at these adressess the concrete values are stored.

Adr  content
1000 a b c 0    // "abc" : string literal '\0' terminated 
1004    1000    // *string: pointer to string (address 1000)
1008      10    // b = 10 : integer value
1012    1008    // *bptr  : pointer to &b 

By using i.g. printf("%s\n" , *string ); you don't want to copy the whole string, rather you give the address where the string starts (call by reference).

stacker
thank you all for your answers :)
Hugo
+1  A: 

A local variable can live in memory, or in a register, or it can float between the two at different stages of program execution, or it can share space with another variable. The compiler is free to allocate the most efficient space for your variable.

If you take a pointer to a variable then the compiler needs to put that variable into memory, so that it has a unique address. But if you never take a pointer to it then your variable can might remain in its own CPU register. Or if you have two local variables, and if you never use both of them at the same time, then the compiler can have them occupy the same piece of memory or CPU register.

http://en.wikipedia.org/wiki/Register_allocation

Tim Robinson
A: 

Not Buddy a pointer is a set of variables, and to differentiate them, besides the number of variables, values (integer, character) has to be different!

Michael
+3  A: 

I know you've already accepted an answer, and this does not directly answer your question... this is for your edification, if you desire to read it.

http://stackoverflow.com/questions/1612982/how-does-automatic-memory-allocation-actually-work-in-c/1613391#1613391

San Jacinto
+4  A: 

A variable is just an abstraction. It is the idea of a named value that you can refer to, read and (sometimes, depending on its type) modify.

Where it is stored in the hardware is just an implementation detail. Often, they are implemented by storing data at a certain memory address, and then using that address whenever the variable is to be accessed, so in that sense, it is often an "automatically dereferenced pointer" as you say.

But sometimes, a variable is stored in a register instead of in memory. Then it doesn't have an address, and you can't create pointers to it.

Sometimes, it may not even exist in the compiled code. Sometimes the compiler might transform the code so the variable is no longer necessary, or the variable might be converted to a single compile-time constant.

Ultimately, variables only exist in the source code. Once your code is executing, variables no longer exist. Some variables are converted into memory locations, and some are removed entirely, or transformed into something you wouldn't even recognize as a variable.

For example, this code:

int x = 10;
y += 10;

could be compiled by representing x and y as memory locations, and then the addition is performed with an instruction such as "add the value from memory address x to the value at memory address y".

But the compiler could also encode the constant 10 into the instruction itself, generating an "add 10 to the value at memory address y" instruction. Sure, x was a variable in the original source code, but it's no longer a memory location. It's encoded directly into the instruction stream.

jalf
A: 

A variable just holds the value it is supposed to hold. If it is a pointer, it will hold a memory address, if it is an integer it will hold an integer value, if it is a float, it will hold a floating point number...

Cold-Blooded
+5  A: 

To read/write a variable is to read/write a piece of bytes at know location. "know location" is a location known by compiler, it can be:

  • Fixed address. Compiler knows all address of global variables. If we read/write a global variable, compiler puts instruction like this: "read/write memory at address 0x00235A87"
  • Fixed stack offset. Local variables are pushed onto the stack. Pointer to the top of the stack ("stack pointer" or "SP") is stored in processor registers. Compiler knows what's the offset of local variable from top of the stack. If we read/write a local variable, compiler puts instruction like this: "read/write memory at address 'SP-0x05'".
  • Processor registers. Variables, when used, are loaded into processor registers. Compiler knows which registers. To use them no memory reading/writing is needed, compiler simply puts instructions that use registers like: "add content of register B to register A".

Accessing a variable is actually algorithm written in a form of processor instructions. Variable can be addresses by fixed memory address, by calculated memory address (using fixed offset and value kept in a register) or processor register. Compiler puts instruction that moves values of variables between memory/register and between register/register. Variable may even not exist in memory, it can be all the time kept in registers.

There is some kind of analogy in that what you say.

Considering above, remember the pointer is variable that keeps an address (which is integral number). If we dereference a pointer and read pointed value then two steps must be done. Firstly we must read pointer variable like any other variable. After that the address is in a register. Then we read pointed variable with instruction like: "read memory at address stored in register A".

adf88