The distribution of C + + variables in memory in the written test often, although simple, but also easy to forget, so make a summary in this to deepen the impression.
Write a test program first:
Copy Code code as follows:
#include <stdio.h>
#include <malloc.h>
int g_i = 100;
int g_j = 200;
int G_k, g_h;
int main ()
{
const int MAXN = 100;
int *p = (int*) malloc (MAXN * sizeof (int));
static int s_i = 5;
static int s_j = 10;
static int s_k;
static int s_h;
int i = 5;
int j = 10;
int k = 20;
int F, H;
Char *pstr1 = "MoreWindows123456789";
Char *pstr2 = "MoreWindows123456789";
Char *PSTR3 = "Hello";
printf ("Data address in Heap: 0x%08x\n", p);
Putchar (' \ n ');
printf ("Data address in stack (with initial value): 0x%08x =%d\n", &i, I);
printf ("Data address in stack (with initial value): 0x%08x =%d\n", &j, J);
printf ("Data address in stack (with initial value): 0x%08x =%d\n", &k, K);
printf ("Data address in stack (no initial value): 0x%08x =%d\n", &f, F);
printf ("Data address in stack (no initial value): 0x%08x =%d\n", &h, h);
Putchar (' \ n ');
printf ("Static data address (with initial value): 0x%08x =%d\n", &s_i, s_i);
printf ("Static data address (with initial value): 0x%08x =%d\n", &s_j, S_j);
printf ("Static data address (no initial value): 0x%08x =%d\n", &s_k, S_k);
printf ("Static data address (no initial value): 0x%08x =%d\n", &s_h, S_h);
Putchar (' \ n ');
printf ("Global data address (with initial value): 0x%08x =%d\n", &g_i, g_i);
printf ("Global data address (with initial value): 0x%08x =%d\n", &g_j, G_j);
printf ("Global data address (no initial value): 0x%08x =%d\n", &g_k, G_k);
printf ("Global data address (no initial value): 0x%08x =%d\n", &g_h, G_h);
Putchar (' \ n ');
printf ("String constant data address: 0x%08x points to 0x%08x content for-%s\n", &PSTR1, Pstr1, PSTR1);
printf ("String constant data address: 0x%08x points to 0x%08x content for-%s\n", &PSTR2, PSTR2, PSTR2);
printf ("String constant data address: 0x%08x points to 0x%08x content for-%s\n", &PSTR3, PSTR3, PSTR3);
Free (p);
return 0;
}
Run results (release version, XP system) are as follows:
You can see:
1. Variables in the memory address distribution is: heap-Stack-code area-Global static-constant data
2. The variables in the same area are allocated in the order of the memory in a declared sequence, from low to high (only an unassigned global variable is an exception)
3. Global and static variables if not assigned, the default is 0. A variable in the stack is a random data if it is not assigned a value.
4. The compiler considers global and static variables to be equivalent, initialized global and static variables, and uninitialized global and static variables are assigned to another.
The above program is all in a main function, the following increases the function call to see the function of the parameters and functions in the allocation of variables where.
The procedure is as follows:
Copy Code code as follows:
#include <stdio.h>
void Fun (int i)
{
int j = i;
static int s_i = 100;
static int s_j;
printf ("Parameters of the child function: 0x%p =%d\n", &i, I);
printf ("Data address in the child function stack: 0x%p =%d\n", &j, J);
printf ("Child function static data address (with initial value): 0x%p =%d\n", &s_i, s_i);
printf ("Child function static data address (no initial value): 0x%p =%d\n", &s_j, S_j);
}
int main ()
{
int i = 5;
static int s_i = 100;
static int s_j;
printf ("Data address in main function stack: 0x%p =%d\n", &i, I);
printf ("Main function static data address (with initial value): 0x%p =%d\n", &s_i, s_i);
printf ("Child function static data address (no initial value): 0x%p =%d\n", &s_j, S_j);
Putchar (' \ n ');
Fun (i);
return 0;
}
The results of the operation are as follows:
It can be seen that the main function in the stack address is higher than the parameters and stack address in the sub function, proving that the extension direction of the stack from the high address to the low address extension. the address of the static data in the main function and the child function is also adjacent, stating that the program assigns the initialized global and static variables together, and that uninitialized global and static variables are assigned together.