Memory Management in C ++

Source: Internet
Author: User
Author: Cao Yifei Source: Fangzhou Abstract:

Everyone knows the concept of variable life cycle, but sometimes the variable life cycle has ended, but the allocated memory space still exists. The article provides four different examples to illustrate the causes.

  Preface:

Some time ago, I saw a small problem on a friend's blog forum that he encountered when learning C ++. It was about the most basic memory allocation in C ++. In fact, in addition to knowing that I use MB of memory, I am not sure how it actually works. After reading the explanations from the masters, I have a deep understanding. I will sort out these explanations to encourage all c ++ newbie!

  Body:

This is because of such a short program.

Int main ()
{
Int I = 10;
Int * j = & I;
If (! 0)
{
Int L = 20;
Int * k = & L;
J = K;
K = 0;
}
Cout <* J;
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.

Int I = 10; // I is 10 and & I is 0x0012ff7c
Int * j = & I; // * j is 10 and & J is 0x0012ff7c
// Obviously, the two variables refer to the same address.
If (! 0)
{
Int L = 20; // L is 20 and & L is 0x0012ff74

/* The address 0x0012ff7c-0x0012ff75 is occupied. It should be noted that,
This value may vary depending on the computer hardware. */

Int * k = & L; // * k is 20 and & K is 0x0012ff74

// The variable K and L point to the same address.

J = K; // J is 0x0012ff74 and * j is 20

/* Assign values between pointers. This statement is to assign the negative value of the address pointed by K to J.
The two variables point to the same address, both of which are 0x0012ff74.
The block address is 20, so * j is 20. */
}

Cout <* j; // * j is 20 and J is 0x0012ff74

/* The K address is 0x00000000, indicating the K variable.
It has been automatically destroyed, so the address is zero. However, J does not refer to K,
Is the address 0x0012ff74 that K refers to, and because J's lifecycle is still
No end (J was accidentally defined in if), so the address that J points to and
If it is not recovered, the number 20 is saved. */

So far, we have analyzed the memory allocation throughout the entire process of the program, and the final result is this. (Figure 1)

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. (Figure 2)

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.

For the above program, we need to change to point the variable J to the location where an address is released. So we have the following program.

Int * Foo ()
{
Int L = 20;
Return & L;
}

Int main ()
{
Int I = 10;
Int * j = & I;
J = Foo ();
Cout <* J;
Return 0;
}

The compiler is "smart". A warning will be given after compilation. The original statement is "returning address of local variable or temporary", pointing to the fourth line of the above program, that is, the Return & L; statement. I don't need to explain the meaning of that sentence. I believe everyone can understand it.

The execution result is 20 in debug and 4198795 in release. Obviously, the part of memory is released. This is because the foo function is executed in the debug program and the address of L is not released immediately (I do not know whether this sentence is accurate ). In the release version of this program, it is clear that the program releases the address, so it points to an uncertain number.

One thing to mention here is that in the first program, both the debug version and the release version. After the if statement is executed, the system does not actually clear l. L is just an alias of K. The above program is written in this way. A negative statement such as * j = & I is used, and the alias interpretation in msdn is the same as the reference, so it can be understood as follows, int I = 10; Int & J = I; is the same as above. Don't think about the above programs. Let's take a look at the following.

Void F1 (int * & J)
{
Int L = 20;
Int * k = & L;
J = K;
K = 0;
}
Void any_function_use_local_variables ()
{
Int A, B, C;
A = B = c = 100;
}

Int main ()
{
Int I = 10;
Int * j = & I;
F1 (j );
Cout <* J;
Any_function_use_local_variables ();
Cout <* J;
Return 0;
}

Compile and execute the command on your own to see what the result is. Then, combine the two examples above to find out why. The following is a small example, which may help you understand the concept of memory.

The process of the program is to try to increase I so that it exceeds the maximum integer. One case is that the value is changed to a negative number by "rolling back". The printed result of the program on my machine is-2147483648, which may vary with hardware.

Int main ()
{
Int I = 1;
While (0 <I) I ++;
Cout <I;
Return 0;
}

End.

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.