How will the value stored in the designated memory address

 

  Suppose now need to address the memory 0x12ff7c into an integer 0x100, then how can we do?

  We know by a pointer address to which data is written at the memory, this memory address is not in essence a 0x12ff7c pointer Well, so we can use the following method:

int *p = (int *)0x12ff7c;
*p = 0x100;

  It should be noted that, when the address 0x12ff7c assigned to the pointer variable p must be cast.

  Here only select memory address 0x12ff7c not choose another address, such as 0xff00 etc., merely for the convenience of testing on Visual C ++ 6.0 only. If you choose 0xff00, then the implementation of "* p = 0x100;" when this statement, the compiler may report a memory access error, because the memory address at 0xff00 and you may have no right to access. There is a problem, how do we know if a memory address can be legally accessed it? That 0x12ff7c know how memory is not accessed at it? In fact, this is very simple, we can define a variable i, for example:  int i = 0 ;  variable i in which the memory certainly can be accessed; then observe on the Watch window of the debugger & the value of i know its memory address a. Here I get the address is 0x12ff7c, nothing more (different compilers may give each variable i allocate memory addresses are not the same, but Visual C ++ 6.0 is just the same every time). You can give any other address can be assigned a legitimate access to obtain this address and then put  int i = 0 ;  this code to delete all the "evidence" was destroyed completely.

  

  In addition to this there is no other way to it? not necessarily. So we can even write code:

*(int *)0x12ff7c = 0x100;

  In fact, this line of code and the above two lines of code is not essentially different. First address 0x12ff7c cast, tells the compiler will store the address of an int data; and "*" write a piece of data to memory by a key.

  

  So much discussed above, the purpose is to tell you, in fact, their form of expression is not important, important is that this way of thinking, a memory address that is to say by the way we are fully specified to write data.

 

Guess you like

Origin www.cnblogs.com/doitjust/p/11318208.html