Pointer Discussion & Memory Leak

Source: Internet
Author: User

1. Declare a pointer

Int * pcount; // a pointer to int variable, named pcount

Int COUNT = 5;

Pcount = & count; // & is the address operator, * pcount = & count is incorrect

Cout <count; // 5

Cout <pcount; // output the memory address stored in count

Cout <& count; // output the memory address stored in count

Cout <* pcount; // 5

Note:

① We can regard int * as a data type, so pcount refers to the pointer itself, and the stored variable is the memory address; * pcount refers to the specific content in the memory address pcount.

② Int * indicates the pointer pointing to an int type. The pointer must be consistent with the data type. The following error occurs:

Eg: Double area = 3.14;

Int * parea = & area; // wrong

Double * Area = & area; // correct

2. pass-by-reference with pointers

Void swap (int * pvalue1, int * pvalue2)

{

Int temp = * pvalue1;

* Pvalue1 = * pvalue2;

* Pvalue2 = temp;

}

Int main ()

{

Int num1 = 1;

Int num2 = 2;

Swap (& num1, & num2); // After swapping, num1 = 2, num2 = 1;

}

Note: two values are exchanged by changing the content of the pointer pvalue1 and pvalue2.

3. Series & pointer

The column itself is address_based; list refers to the address of the first element, and the address of each element is incremented by 1;

Int list [8] = {1, 2, 4, 5, 6, 7, 8 };

Int * plist = List; // This sentence is equivalent to int * plist = & list [0]

The address of the first element is list, & list [0], or plist. The second element is list + 1, & list [1], or plist + 1; the third element is list + 2 or & list [2] Or plist + 2 ......

4. Constant pointers and pointer Constants

Constant pointer: the pointer cannot be changed, that is, the address cannot be changed, but the content stored in the address can be changed.

Pointer constant: the pointer content cannot be changed, but the address used to store it can be changed.

Const int * pvalue = & value; // pointer constant. * pvalue is a constant, but pvalue can be changed.

Int * const pvalue = & value; // constant pointer. pvalue is a constant, but * pvalue can be changed.

5. Return pointer

We know that we cannot return an array and can only pass one array. But we can return a pointer. The problem is that the pointer declared in the function is local, and the stack is cleared immediately after the return, therefore, we must allocate pointer space in heap so that the returned data can be passed to the main function.

① Declaration: Use new to dynamically allocate array space

Int * reversearray (const int * list [], int size)

{

Int * resutarray =New int [size];// You can use resultarray [0] to access the new series.

/*** Main function body ***/

Return resultarray;

}

NOTE: If it is a single variable, you can declare it as follows:

Double * test = new double; // allocate a memory space to the double variable * test, and assign the memory address to test.

Release Space

The newly dynamically allocated memory must be deleted using Delete. Otherwise, memory leakage may occur.

Int * pvalue = new int; // assume that the IP address 1005 is allocated to pvalue dynamically.

* Pvalue = 45;

Pvalue = new int;

In this way, if you forget to release the original pvalue memory space, you will immediately allocate a new one to it. As a result, because no other Pointer Points to the address 1005, this memory cannot be accessed or modified again, this causes memory leak !!

Therefore, we must use the following statement:

Delete [] resultarray;

Or

Delete pvalue;

③ About Memory Leak

What? It must be explained from the perspective of the operating system that the space allocated by the operating system to all processes is being squeezed out by a process, and a piece of memory requested by the program. If no pointer points to it, this memory is leaked, and it cannot be accessed or modified again;

Consequence? If the code is a one-time (executed once), it will not be harmful; if the code is sporadic (Executed randomly), Common (Executed frequently) Or even implicitly (Executed implicitly), So each execution will cause a piece of Memory leakage, so that all the memory will be occupied eventually, leading to system crash, especially Implicit Memory leakage, which is difficult to monitor, infinite harm.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.