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.