Chapter XIII unusual data types

Structure

Reasons to Use structure:

  • With a clear structure to data relationships;
  • Simplify the operation of the structure for the data block;
  • To simplify the structure for the list of parameters;
  • With a structure to reduce maintenance.

pointer

General skills using a pointer

Proper use of pointers require the programmer to use a two-way strategy. First and foremost, to avoid an error pointer; the second, the code is written, the pointer error is detected as soon as possible.

  • In the pointer operation limitation subroutine or inside the class;
  • Meanwhile pointer declarations and definitions;
  • Delete pointer pointer assigned the same scope;
  • Delete pointer before using the pointer;
  • Check referenced by the pointer variable to use it;
  • With a dog tag field to detect damage to the memory;
  • A significant increase in redundancy;
  • With additional pointer variable to improve the clarity of the code;
  • Simplify complex pointer expression;
  • Draw a map;
  • Delete list pointer in the correct order;
  • A reserve allocated memory area reserved;
  • Crushed garbage data;
  • After you delete them or release the pointer is set to null;
  • Check the invalid pointer before deleting variables;
  • Tracking finger allocation;
  • Writing cover routine, focus on achieving avoid pointer solving strategies;
  • Techniques using non-pointer.

C Pointer

C language pointer tips:

  • Using explicit pointer type instead of the default type;
  • Avoid cast;
  • Follow asterisk rule passed as a parameter;
  • Memory allocation used sizeof () determine the size of the variable.

Global Data

Global data can be accessed at any point in the program. Using global data risky than using local data, access data through a number of useful routines.

Frequently Asked Questions related to Global Data

  • Inadvertently modify the global data;
  • Exotic and exciting issues related to Global Data;
  • Global data codes associated with the problem of re-entry;
  • Global Data hinder code reuse;
  • Initialization sequence matters related to global data of the non-deterministic;
  • Global data undermined the manageability of the modularity and intelligence.

The reason for using global data

  • Save Global Data;
  • Analog named constants;
  • Analog enumerated types;
  • Its ease of use of common data;
  • Eliminate stray data.

Only as a last resort the use of global data

  1. Each of the first local variable is set to, only when needed to set the global variable;
  2. Distinguish global variables and class variables;
  3. Question put to use subroutines.

Substituted with access routines to global data

Use access routines is a core method of information hiding and abstract data type implementations.

  • Use access routines advantage;
    • You get centralized control of data;
    • You can ensure that all references to the variables have been protected;
    • You can automatically obtain the benefits of universal information hiding;
    • Access routines can easily be transformed into abstract data types.
  • How to use access routines;
    • All the code required to access the data access routines;
    • Do not put all your global data are thrown in one;
    • By locking to control access to global variables;
    • Build an abstraction layer in your access routines in;
    • So that all access to the item of data takes place on the same level of abstraction.

How to reduce the risk of using global data

  • The creation of a naming convention to highlight global variables;
  • Creating an annotated list of good for the whole global variables;
  • Do not use global variables to store intermediate results;

More resources

Maguire《Writing Solid Code》

Meyers《Effictive C++》

Checklist: Note the use of unusual data types

Structure

  • [] You use a structure instead of a simple variable to the organization and operation of the relevant data?
  • [] You consider creating a class structure to replace it?

Global Data

  • [] Whether all the variables are local or class-wide? Unless absolutely necessary is global?
  • [] Naming variables can local data, class data and global data to distinguish it?
  • [] You do have to be documented for all global variables?
  • [] Avoid using pseudo global data, ie four passes and contains a huge messy data object has it?
  • [] To replace it with global data access routines?
  • [] The organization access routines and data into the class it?
  • [] Access routines provide a layer of abstraction over the underlying data type implemented in it?
  • [] All the relevant access routines in the same layer of abstraction it?

pointer

  • [] The isolation pointer operations in the subroutine do?
  • [] Pointer reference legal? Or pointer may become dangling pointers do?
  • [] Code checks its validity before you use the pointer?
  • [] It is checked for validity before using variable pointer is pointing?
  • [] After the pointer to null it run out?
  • [] On the terms of readability, the code used all pointer variables need to use it?
  • [] Linked list pointer is to be released in the correct order it?
  • [] Program allocates memory to retain a reserve area in order to be able to exit gracefully when it runs out of memory?
  • [] Is not the case in no other way available before the final use of pointers?

Points

  • The program structure can be made simpler and easier to understand, and easier to maintain;
  • Whenever you are going to use the time structure, it is not considered class will work better;
  • Pointer error prone. With access routines or classes and defensive programming practices to protect their own code;
  • Avoid using global variables, not just because they are very dangerous, because you can still use other better ways to replace them;
  • If you have to use global variables, then use it by access routines. Access routines can bring everything a little bit, there are some additional benefits of global variables that can bring you.

Guess you like

Origin www.cnblogs.com/liam-ji/p/11528641.html