A summary of the pointer and memory problems that plagued me two days ago

Source: Internet
Author: User
# Include <iostream>
Using namespace STD;

Void fuck (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;

Fuck (j );
/////-------------------------
Cout <* j <Endl;
////-------------------------
Cout <* j <Endl;

Any_function_use_local_variables ();
////-------------------------
Cout <* j <Endl;
//
Cout <* j <Endl;

Return 0;
};

Print the result. The result of the four cout <* j <Endl; operations are different:
20
274148040
-858993460
274148040
At the beginning, I didn't quite understand why the local variable fuck still got 20 values. j should point to an uncertain memory area, but there were several different results.
So I posted it in the forum, and I checked a lot of materials and tried a lot.Program.
The reason is basically as follows:
The first output is just returned, although local variables are destroyed.
(My understanding is destroyed: I gave up this place. I put an apple there and the apple was still there when no one was using this place)
So the first output is still 20. This memory should be used later, so it becomes another value.
Below is a response from a cool man, which makes sense!
///////////////////
First output: in the Void fuck (int * & J) function, the declared local variable k is assumed as the address oxff71. In the function, this address is assigned to J;
Then, the value of the memory referred to by the local variable K in the function is 20. Then, the fuck function ends and the memory of the local variable is released. The single release only indicates that the memory can be reused, the value in the memory is not 0, so. In the first output, * j is 20;

2nd output: in fact, cout <is also a function called. I don't know how to implement this function, but I have reason to believe that some local variables are used in this function, after * j is output for the first time, this function may not end. The local variables in the function use the memory just released by fuck, however, some local variables use the oxff71 address, but the address referred to by the variable J is also like this, so another group of numbers is output.

3rd output: After the any_function_use_local_variables () function is called, we can simplify the any_function_use_local_variables function to the following:
Void any_function_use_local_variables ()
{
Int A = 10;
Int B = 100;
}
PassCodeWe can see that the any_function_use_local_variables function still uses the memory released by fuck, because we can clearly see that the address of B is oxff71, so 3rd output is 100.

In fact, we can add a cout <* j <Endl after three cout <* j <Endl. We can see that 4th outputs and 2nd outputs are the same, this proves my conjecture about the 2nd output.

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.