"21 days to learn C + +" 8th chapter on Pointer Learning notes

Source: Internet
Author: User

1. C + + A powerful and low-level tool is the ability to manipulate computer memory directly using pointers , which is one of the advantages of C + + with respect to languages such as Java, C #, and Visual Basic.

2. What is a pointer? pointers are variables that store memory addresses.

3. Being able to use pointers and manipulate memory at the bottom is one of the reasons C + + is chosen to write embedded and real-time applications.

4, the pointer when the definition must be initialized, the pointer is not initialized is called a runaway pointer, is very dangerous. int *page=0; The page is initialized to 0, and a pointer with a value of 0 is called a null pointer.

5, pointers and Arrays: in C + +, the array name is a constant pointer to the first element of the array. For example, int number [5];number is a pointer to the first element of the array number number[0], so number+4 is a legitimate way to access number[4].

6. array pointers and array of pointers:

int numbersone[500];

int *numberstwo[500];

int *numbersthree=new int[500];

Where: Numbersone is an array containing 500 int objects, Numberstwo is an array containing 500 int pointers , Numbersthree is a pointer to an array containing 500 int objects.

7. When to use pointers: pointers are often used to accomplish the following 3 tasks,① managing data for free storage areas ② accessing the class's member data and functions ③ passing parameters by reference (pointer passing variables are referred to as by reference)

8. Buyers and free Storage (heap): 5 Memory areas: Global variable space, code space, stack, register, free storage (heap)

Global variables are in the global namespace, the code is in the code space, local variables and function arguments are on the stack, registers are used for internal management, such as buyers top pointers and instruction pointers, and all remaining memory is used as free storage, often referred to as heaps.

9. the advantage of a free storage area is that the memory you allocate from is always available until you explicitly indicate that you no longer need to release it. If the memory of the free storage area is allocated in the function, the memory is still available after the function returns.

This is also a disadvantage of free storage: If you forget to free up memory, the memory that is occupied and unused is more and more accumulated over time, causing the system to crash.

The advantage of using this kind of memory access instead of a global variable is that only a function that has access to the pointer can access the data it points to. This way, the function can modify the data that the pointer points to by passing the object or the pointer itself to the function, reducing the situation where the function can modify the data when the global variable is used and cannot track the change.

10. How to allocate memory in a free storage area: in C + +, use the keyword new to allocate memory for a free storage area , such as creating a unsigned short variable in a free storage area, you can do this:

unsigned short int * ppointer; Ppointer =new unsigned short int;

This opens up a space, and Ppointer will point to a unsigner short int in a free storage area that can assign a value to the memory area it points to: *ppointer = 72;

The return value of new is an address, so assign the return value of new to a pointer.

Use the keyword Delete to return memory:

The memory allocated with new is not automatically freed, in order to prevent memory leaks, the allocated memory is returned to the storage area with the keyword. such as: delete ppointer; When you delete a pointer, you actually release the memory that is stored in the pointer, which is equivalent to returning the memory that the pointer points to to the free storage area, which still exists and can be re-assigned.

11, Memory leaks: memory leaks are also known as "storage leakage", with dynamic storage allocation function dynamically open space, after use has not been released, the result has been occupied in the deposit element. Until the end of the program. (In fact, the memory space is not recycled after use) is called a memory leak.

Talk about memory leaks: Another serious scenario that could lead to a memory leak is that the memory that it originally pointed to was not released until the pointer was assigned a value.

1  Short int* ppointer=newshortint; 2 *ppointer=; 3 ppointer=newshortint;//This line causes a memory leak 4 *ppointer =n/A;

Line 3rd, 4 will cause a memory leak because the original block of memory after line 3rd (the stored value is 72) is not available because a pointer to that memory block has been re-assigned in the third row, and now cannot access the memory block that originally stored 72, and it cannot be released until the program ends, thus causing a memory leak.

It would be nice to add a line to release the pointer code.

 Short int* ppointer=newshortint; *ppointer=; Delete ppointer;//releases the memory block originally pointed to ppointer=newshortint; *ppointer=;

Note: Each new in the program should have a corresponding delete.

12. create an object on a free storage area:

Just as you can create pointers to shaping, you can also create pointers to any data types, including classes. For example, if you have a cat class, you can declare a pointer to the cat class and instantiate a cat object in a free storage area:

Cat *pcat= New Cat;

13, the Lost pointer : The delete is used for the pointer (thereby releasing the memory it points to), and then does not set it to null when it throws a stray pointer, causing unpredictable consequences.

So the delete is used to set the pointer to 0 (a null pointer).

That is: Cat *pcat =new cat;

Delete Pcat;

pcat=0;. Delete is used after the pointer to set it to 0 (null pointer)

14. Use the const pointer: when declaring a pointer, you can use the keyword const before or after the type, or you can use it in both places:
const int *pone;

int *const ptwo;

const int* Const Pthree;

①pone is a pointer that only wants to reshape a variable, and the value it points to cannot be modified.

②ptwo is a variable pointer to an integral type that points to a value that can be modified, but ptwo cannot point to another variable.

Pthree is a constant pointer to an integer constant, and the value it points to cannot be modified, and the pointer cannot be just another variable.

Memory Skills: The const right is a type, the value is a constant, and a const right is a pointer variable, the pointer itself is a constant and cannot point to another variable.

21 days Learn C + + 8th chapter on pointer Learning notes

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.