C ++ Memory Management (1)

Source: Internet
Author: User

StartC ++ Memory ManagementDiscuss:

First look at a program:

 
 
  1. int main()  
  2. {   
  3. int i=10;   
  4. int *j=&i;   
  5. if(!0)  
  6. {   
  7. int l=20;  
  8. int *k=&l;  
  9. j=k;  
  10. k=0;  
  11. }  
  12. cout<<*j;  
  13. return 0;  

No compiler. Do you think about what results should be printed after execution? I think the first response is to print an uncertain number. The reason is that in the if statement, we define the k variable. After the if statement is executed, the memory occupied by k is reclaimed by the system, as a result, the result indicated by variable j is very uncertain. Of course, after compilation and execution, we find that the final print result of the program is 20, which is not an uncertain number we expect. Let's analyze the cause!

We use the debug Method for step-by-step analysis, and input all the variables in the watch window.

 
 
  1. Int I = 10; // I is 10 and & I is 0x0012ff7c
  2. Int * j = & I; // * j is 10 and & j is 0x0012ff7c
  3. // Obviously, the two variables refer to the same address.
  4. If (! 0)
  5. {
  6. Int l = 20; // l is 20 and & l is 0x0012ff74
  7.  
  8. /* The address 0x0012ff7c-0x0012ff75 is occupied. It should be noted that,
  9. This value may vary depending on the computer hardware. */
  10.  
  11. Int * k = & l; // * k is 20 and & k is 0x0012ff74
  12.  
  13. // The variable k and l point to the same address.
  14.  
  15. J = k; // j is 0x0012ff74 and * j is 20
  16.  
  17. /* Assign values between pointers. This statement is to assign the negative value of the address pointed by k to j.
  18. The two variables point to the same address, both of which are 0x0012ff74.
  19. The block address is 20, so * j is 20. */
  20. }
  21.  
  22. Cout <* j; // * j is 20 and j is 0x0012ff74
  23.  
  24. /* The k address is 0x00000000, indicating the k variable.
  25. It has been automatically destroyed, so the address is zero. However, j does not refer to k,
  26. Is the address 0x0012ff74 that k refers to, and because j's lifecycle is still
  27. J is accidentally defined in if), so the address that j points to is
  28. If it is not recovered, the number 20 is saved. */

At this point, we have analyzed the memory allocation throughout the entire process of the program. The final result is as follows:

 

We can also look at the specific content of this address in Memory. We can see that it is 14, which is a hexadecimal number, converted to decimal, exactly 20. As shown in:

 

Now you should have a rough understanding of the execution process of the above program! However, this is not the expected result. What we need is to print an uncertain result. With the above analysis, we started a new program and asked him to print what we wanted.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.