Thinking of variable memory allocation and return pointers and reference types in functions in C + +

Source: Internet
Author: User

As we all know, we often declare local variables (including normal types of variables, pointers, references, and so on) in our functions when we are programming.

Also, the return value of a function is often a pointer type or reference type in order to satisfy the needs of the program, and the returned pointer or reference also often points to a local variable that we declare ourselves in the function.

In this way, the program may have certain problems in some cases. Seemingly simple problem, through careful analysis, we will be able to better understand the memory allocation and release in C + + issues.

Well, don't say much nonsense, we get to the point. First, briefly describe the allocation of memory areas of the program:

Memory allocation of the program

① Heap Area (heap). This section is primarily a memory area that is saved by an "object" that is created by the program developer themselves through operations such as new and malloc, and the variables for that zone need to be freed by the developer themselves. Stored like a linked list.

② stack area (stack). This part is mainly the memory area (including the main function) occupied by the function in the program and the variables in it. stored in a similar way to a stack.

③ the global or Constant zone (static). This section is mainly about storing global variables, static data, and constants. Released by the system after the program is finished.

④ literal constant area. This part contains the constant string except the demon. Released by the system after the program is finished.

⑤ the program code area. Binary code of the program.

1. Declare common variables in functions

void func1 () {    int i;} 

In the function func1 (), we declare an int variable i, which is stored in the stack area, and I will be freed when the func1 execution is complete.

2. Declaring a pointer-type variable in a function

void Func2 () {    int *p = (int*) malloc (sizeof (int));    A *a = new A; A is a class} 

In function Func2 (), we declare an int pointer variable p and a user-defined class A pointer variable a, which are all stored in the heap, and are still not freed after the function Func2 is executed, and need to be released by the program developer themselves using free () or delete, and so on. Failure to do so will cause a memory leak problem.

3. Function return value is pointer or reference type

The return value of a function is very common and useful for pointers and reference types, and there is no problem if our function returns a pointer or refers to a pointer-type variable declared in the function. As follows:

int* createpointerfactory () {    int *p = (int*) malloc (sizeof (int));    To does STH here    return p;}  

Such code is also a common simple factory pattern. However, it is important to note that after the pointer is obtained through the function createpointerfactory, the corresponding memory release work is done after the relevant operation has been completed. Finally, you need to set the value of the pointer to null to prevent the wild pointer from being generated.

In the second case, a pointer returned by a function or a reference from a normal type variable declared in a function can cause a very serious problem. As follows:

int* Getpointer () {    int i = 1;    To does STH here    return &i;} main.cppint* p = getpointer ();  

Because the variable i is created in the stack area, after the execution of the Getpointer function, the memory area will be freed and the memory of the variable I will be freed. Therefore, if we call the function somewhere else, such as in the main function, then the returned pointer is very dangerous, and the point to the position is meaningless.

However, when doing the experiment, we will find that at this time, under some compilers, the value of printing *p may still be 1, some may be garbled.

The main reasons for this are as follows:

① different compiler processing and optimizations are different.

②os after releasing this part of the memory, it does not completely empty the memory data, but only the flag bit flag is true, indicating that this block of memory is not occupied.

Thinking of variable memory allocation and return pointers and reference types in functions in C + +

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.