Chapter 4 notes: Uncommon Data Structures

Source: Internet
Author: User
Pointer:

In terms of concept, each pointer contains two parts: a location in the memory and how to explain the content in the location.
Tips for using pointers: To solve many types of program errors, the easiest part is to locate the errors and the most difficult part is to correct the errors... Most of the work of correcting a pointer error is to find its location.
Several correct statements.1 Restrict pointer operations in subprograms or classes?Assume that you have used a linked list multiple times in the program. Instead of traversing it by manually operating the pointer, you should write a set of incoming nextlink (), previouslink (), this kind of accessors subprogram is better to complete the operation. 2 It is usually a good programming practice to assign an initial value to the variable close to the variable declaration. It is initialized for the definition or declaration. 3. Check the pointer before using the pointer. In fact, this can be achieved with the previous pointer operator. 4. Check the variable referenced by the pointer before using it. Sometimes you should check the rationality of the data pointed to by the pointer. 5. The obvious increase in redundancy means that some specific fields Repeat twice. If the data in the redundant field does not match, you can confirm that the data has been damaged. 6. Use additional pointer variables to improve code clarity. Do not use pointer variables for multiple purposes, that is, do not use the same variable for multiple purposes. 7. simplify complex pointer expressions, such as P-> q-> r-> S. data to increase readability 8. It is difficult to debug pointer errors because you cannot determine when the memory pointed to by the pointer becomes invalid. Sometimes the pointer has been released for a long time, and the corresponding memory content still looks valid. Therefore, in the C language, junk data is used to overwrite the memory areas before the memory areas are released, which makes the expressions of released pointer-related errors more consistent. 9. delete or release pointers and set them to null values. 10 check for invalid pointers before deleting variables. This should be combined with 9th. In the following example, we can maintain a list of allocated space pointers by combining and 10 with 11 tracking pointer distribution. For global variables, use an access subroutine to replace global data. The anti-question subroutine is an example of information hiding. The anti-bot subroutine allows you to redecorate the interior without changing the appearance of your house. For example, g_globalstatus is used to describe the program state. You can create two accessors: globalstatus. get () and globalstatus. set (). therefore, it is required that all codes use accessors to access data. How can we reduce the risk of using global data? 1. Create a naming rule to highlight global variables so that local data can be stored, data classes are separated. 2. Create a well-noted list for all global variables. 3. Do not use the global variable to store the intermediate result pointer:

In terms of concept, each pointer contains two parts: a location in the memory and how to explain the content in the location.
Tips for using pointers: To solve many types of program errors, the easiest part is to locate the errors and the most difficult part is to correct the errors... Most of the work of correcting a pointer error is to find its location.
Several correct statements.1 Restrict pointer operations in subprograms or classes?Assume that you have used a linked list multiple times in the program. Instead of traversing it by manually operating the pointer, you should write a set of incoming nextlink (), previouslink (), this kind of accessors subprogram is better to complete the operation. 2 It is usually a good programming practice to assign an initial value to the variable close to the variable declaration. It is initialized for the definition or declaration. 3. Check the pointer before using the pointer. In fact, this can be achieved with the previous pointer operator. 4. Check the variable referenced by the pointer before using it. Sometimes you should check the rationality of the data pointed to by the pointer. 5. The obvious increase in redundancy means that some specific fields Repeat twice. If the data in the redundant field does not match, you can confirm that the data has been damaged. 6. Use additional pointer variables to improve code clarity. Do not use pointer variables for multiple purposes, that is, do not use the same variable for multiple purposes. 7. simplify complex pointer expressions, such as P-> q-> r-> S. data to increase readability 8. It is difficult to debug pointer errors because you cannot determine when the memory pointed to by the pointer becomes invalid. Sometimes the pointer has been released for a long time, and the corresponding memory content still looks valid. Therefore, in the C language, junk data is used to overwrite the memory areas before the memory areas are released, which makes the expressions of released pointer-related errors more consistent. 9. delete or release pointers and set them to null values. 10 check for invalid pointers before deleting variables. This should be combined with 9th. In the following example, we can maintain a list of allocated space pointers by combining and 10 with 11 tracking pointer distribution. For global variables, use an access subroutine to replace global data. The anti-question subroutine is an example of information hiding. The anti-bot subroutine allows you to redecorate the interior without changing the appearance of your house. For example, g_globalstatus is used to describe the program state. You can create two accessors: globalstatus. get () and globalstatus. set (). therefore, it is required that all codes use accessors to access data. How can we reduce the risk of using global data? 1. Create a naming rule to highlight global variables so that local data can be stored, data classes are separated. 2. Create a well-noted list for all global variables. 3. Do not use global variables to store intermediate results.

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.