Home > Back-end >  C language tutorial of some questions about the virtual address, consult?
C language tutorial of some questions about the virtual address, consult?

Time:10-04

The original
In C, the value of the pointer variable is a memory address, & amp; The role of the operator is also variable memory address, please see the following code
 
# include & lt; stdio.h>
# include & lt; Stdlib. H>
Int a=1, b=255;
Int main () {
Int * pa=& amp; a;
Printf (" pa=% # X, & amp; B=% # X \ n ", pa, & amp; B);
system("pause");
return 0;
}


In C - Free run under 5.0, the result is:
Pa=0 x402000, & amp; B=0 x402004

Code of a, b is a global variable, their memory address at link time has decided, later any further can't change, whenever the program running, the result is the same,

Above is the original code, and then I saw the other articles on the net, they run every time the output results are the same,
But every time I run to get the address is change, how be to return a responsibility?
The following is my run results, every time is not the same:
Pa=0 x1278034, & amp; B=0 x1278038
Pa=0 x368034, & amp; B=0 x368038
Pa=0 xfe8034, & amp; B=0 xfe8038

CodePudding user response:

Each run, the allocation of memory location is not the same as bai, that's normal ~ your computer has been running the program, may be the last time to use the location of the for use by other applications, it is possible,

CodePudding user response:

reference 1/f, confident boy reply:
every run, the allocation of memory location is not the same as bai, that's normal ~ your computer has been running the program, may use the position of the last time for use by other applications, it is possible,


Output of the above are virtual address rather than the physical address, that means the storyteller said is wrong? Impossible to each run output address is the same,

CodePudding user response:

Trying to figure out this problem, need to know about the structure of the executable file

CodePudding user response:

Don't run in the debug, double-click the file to run directly,

CodePudding user response:

reference 4 floor GKatHere response:
don't run in the debug, double-click the file to run directly,


Double-click the file to run directly and the results are different each time

CodePudding user response:

Your book is wrong, after the completion of the compilation, decided to only a, b's relative address (relative to the base address), and absolute address is not guaranteed, in fact, modern operating systems will be deliberately changed the location of the base, in order to make a, b absolute address change, after a special configuration or older operating system is not support this address the operation of the randomization,
Note: only the C language standard a, b in a separate each address never changes during the program execution, but not guarantee multiple executable program also can keep the same address,
  • Related