开发者

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

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开发者_JAVA百科 I think that a variable is a pointer that is dereferenced automatically when used... does that make any sense?


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


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


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


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.


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.

How does automatic memory allocation actually work in C++?


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


A variable is a name that refers to a location. The location is resolved at compile time - the compiler figures out the location at compile time, and will replace all variables with their respective locations. Basically, each time the compiler finds a variable definition, it puts the name in a so called symbol table. It has at least two columns: the name (primary key if you will), and a location. To put it simply, when the compiler has processed all variables, and figured out their locations, the compiler will swap out all variable references with their respective locations. (There's more to it that this, but that's a book worth of material...)

Pointers are variables too. What makes a pointer useful, is that the contents stored in the location of the (pointer) variable, can be used to read or write values in a different location. This is what's called dereferencing the pointer. This is done at run time. In this respect, you can't really say that variables that are automatically dereferenced, because the work has been deferred from compile time to run time.


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!


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜