Mosquito 132 C + + in the function of local variable pointer is how to release it? __jquery

Source: Internet
Author: User
Tags function definition

Original: Mosquito 132 C + + in the function of local variable pointer is how to release it.

Mosquito 132 often encounter problems, the function of local variables when the pointer will be wrong. Mosquito 132 C + + The function of local variable pointer is how to release it.
Mosquito 132 the memory of a variable defined within a function is emptied when the function returns, so if the return value is a local variable, a null value is returned.
To tell you the details:
When the compiler generates code for the function call, it will first press all the parameters of the stack, and then the return address into the stack, and finally for local variables in the stack to open up space, the structure is as follows (stack top above):
function arguments
return address
Local variables
When the function returns, the stack pointer moves from the local variable to the return address, at which point the memory of the local variable is emptied.
This does not occur for the program language built-in type (int, float, and so on), which is primarily present in a custom type or pointer. You can resolve this by prolonging the life cycle of local variables or by saving local variables in two ways.
Extend local variable lifetime: Modifies local variables with static or const.
To save a local variable:
int num;
Store (&num);

Store function definition

void Store (int *p)
{
int i = 5;
*p = i;
}
This saves the value of I in the variable p.

You can also save by using global variables.


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.