Storage of variables in C
1. If the local variables in the function are not specifically described, they are all auto, that is, they are dynamically stored, and auto can be omitted. A local variable can also be defined as static, and its value in the function remains unchanged.
2. If the static local variable is not assigned an initial value, the system automatically assigns a value of 0 during compilation. If the dynamic local variable is not assigned an initial value, its value is an uncertain value.
3. C stipulates that the array can be assigned an initial value only when global variables and local static variables are defined. // Do not understand
4. To improve execution efficiency, c allows local variable values to be placed in registers. This type of variable is called the register variable, which must be described by register. However, only local dynamic variables and formal parameters can be used as register variables.
52) storage of global variables
Global variables are defined outside the function and allocated to the static storage area during compilation. They can be referenced by various functions in the program. How do I reference global variables when multiple files exist? If a global variable is defined in a file and referenced in another file, the global variable must be described using extern in this file, but if the global variable is defined using static, this global variable can only be referenced in this file, and cannot be referenced by other files.
/*
* Test_memory.c
*
* Created on: 2009-10-3
* Author: lengyuex
*/
# Include
# Include
# Include
Static int d = 9;
Int z = 9;
Int * p = & z;
Int;
Static int B = 10;
Static int c;
Void swap (int * x, int * y)
{
Static int d = 8;
Int temp;
Temp = * x;
* X = * y;
* Y = temp;
Printf ("in swap () the d = % d \ n", d );
}
Int main ()
{
Printf ("no define in main, the d = % d \ n", d );
Static int d;
Int e;
Int f [] = {1, 2, 3, 4, 5 };
Int I;
For (I = 0; I printf ("the f [I] = % d \ n", f [I]);
Int t [5];
For (I = 0; I t [I] = I + 2;
For (I = 0; I printf ("the t [I] = % d \ n", t [I]);
Int x = 4, y = 5;
Printf ("before swap () a = % d, B = % d, c = % d, z = % d, d = % d \ n", a, B, c, z, d );
Printf ("the no init e = % d \ n", e );
Swap (& x, & y );
Printf ("after swap () d = % d \ n", d );
Printf ("x = % d, y = % d, z = % d, w = % d \ n", x, y, z, B );
Return 0;
}
Output:
[Lengyuex @ Tux algorithm] $./a. out
No define in main, the d = 9
The f [I] = 1
The f [I] = 2
The f [I] = 3
The f [I] = 4
The f [I] = 5
The t [I] = 2
The t [I] = 3
The t [I] = 4
The t [I] = 5
The t [I] = 6
Before swap () a = 0, B = 10, c = 0, z = 9, d = 0
The no init e = 11472064
In swap () the d = 8
After swap () d = 0
X = 5, y = 4, z = 9, w = 10
Where are the variables stored. The heap is provided by the C/C function library. Its mechanism is very complicated, for example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (specific algorithms can refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. Obviously, the heap efficiency is much lower than the stack efficiency.
The pointer in C/C ++ is actually a variable, which is similar to other types of variables. It is a variable that occupies four bytes (on a 32-bit machine ), it differs from other variables in that its variable value is a memory address that points to another part of the memory. Reference is an alias, which is similar to alias in linux. Furthermore, a pointer variable can point to NULL, indicating that it does not point to any variable address, but the reference must be bound to an existing variable at the time of declaration, in addition, such binding cannot be changed.
In the C language, everyone must be familiar with the & symbol.
In addition to bitwise operations, it also provides more common functions-getting variable addresses.
Let's take a look at the following simple example:
# Include
Int main (void)
{
Int a = 0;
Int * p = &;
Printf ("The value is: % d \ n", * p );
Return 0;
}
In the above Code, the pointer p points to the address of variable. In C/C ++, each variable has its corresponding address. You can get the address of the variable by adding the & symbol before the variable identifier.
So can we write this? Int * p = & 0x01000;
This is obviously not acceptable. For a numeric constant, it has no address. The reason why a variable has an address is that it requires a storage unit to identify the variable (of course, the variable can also be directly mapped to a register ).
Let's look at the following code:
# Include "stdio. h"
Int main (void)
{
Int a = 0; // & a = 0x0012ff60
Int * p = & * (int *) 0x0012ff60;
Printf ("The value is: % d \ n", * p );
Return 0;
}
What is the above Code?
We have previously investigated the address of variable a -- 0x0012ff60, so the pointer p here actually points to the address of variable.
First, use 0x0012ff60 as the int *. In this case, it is equivalent to &.
Then * (int *) 0x0012ff60 indicates getting the content of variable.
Finally, "& * (int *) 0x0012ff60" indicates that the X (int *) 0x0012ff60 is removed, which is equivalent to (int *) &.
Therefore, the & here is different from the & In the first example. Here & is not the address, because a * (int *) 0x0012ff60 is not a variable and has no address. During compilation, the compiler creates a symbol table for each variable identifier, which stores various attributes related to the variable identifier, such as the type and address identifier. The logical address value can be determined after the connection. In short, & acts as an address fetch operation. If & is followed by a variable or function identifier. So here '&' indicates removing the Unquote.
As a result, we can conclude that the behavior result is determined at the time of Compilation When & is used as the address fetch operation, and *, The unreference operation (or content fetch) operation, the behavior result can only be determined at runtime.
Let's take a look at the following example to deepen our impression:
# Include "stdio. h"
Int main (void)
{
Int a = 0;
Int * p = & * &;
Printf ("The value is: % d \ n", * p );
Return 0;
}
In C ++, & can also represent references. This is not much to say.
# Include "iostream"
Using namespace std;
Int main (void)
{
Int a = 0;
Int & r =;
Cout return 0;
}
Address: http://blog.csdn.net/wanghua305/archive/2009/07/17/4357103.aspx