Programming language style overview (bottom)

Source: Internet
Author: User



Seven, the Hands

Eliminate Wild hands

The "wild Pointer" is not a NULL pointer, it is a pointer to "junk" memory. It is generally not wrong to use a NULL pointer because It is easy to judge with an if statement. But a "wild pointer" is dangerous, andtheif statement does not work on it.

There are two main causes of the "wild pointer":

( 1 The pointer variable is not initialized. Any pointer variable that has just been created does not automatically become a NULL pointer, and its default value is random, and it can be arbitrary. Therefore, the pointer variable should be initialized at the same time it is created, either by setting the pointer to NULLor bypointing it to legitimate memory. For example

char *p = NULL;

Char *str = (char *) malloc (+);

( 2 ) Pointer P was Free or Delete after that, it is not placed as NULL , people mistakenly think that P is a legitimate pointer. See section 7.5 .

( 3 The pointer operation goes beyond the scope of the variable. This is an impossible situation, and the sample program is as follows:

class A

{   

Public

void Func (void) {cout << "Func of Class A" << Endl;}

};

void Test (void)

{

A *p;

        {

A;

p = &a;// Note the lifetime of a

         }

P->func ( ); //P is "wild pointer"

}

function Test in the EXECUTE statement P->func () when the object a has disappeared, and P is to point a of, so P is a "wild pointer". But the strange thing is that I ran this program without error, which may be related to the compiler.

Viii. notes

1 , Segment Comment /*...*/, line comment //, and document comment /**...*/

Annotations are generally applied to:

( 1 ) version, copyright notice;

( 2 ) function interface description;

( 3 ) important lines of code or paragraph hints.

Although annotations can help you understand your code, be careful not to use annotations too much. The number of comments should generally be one-third of the code.

2 , "0" value of the judgment

Boolean type variable applicationtrueand thefalserepresents, without applying1or0, inClanguage,C99version beforetrueand thefalsenot recognized, butC99after adding<stdbool.h>This header file, you can usetrueand thefalse.

BOOL The form of the type variable

if (flag )

if ( !flag)

The judging form of pointer type variable

if ( p = = NULL)

if (p! = NULL)

3 , opening and closing of ports

The opening and closing of the port to follow the principle of where to open, otherwise it will result in the consumption and waste of resources. Although it is difficult to achieve complete switch correspondence in actual development, this is also the main reason of memory leakage, but to release the used resources as much as possible to ensure the maximization of resource utilization.

Resources:

"high-quality C + + Programming" Lin Rui 2001

"Ten Programming Vices"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Programming language style overview (bottom)

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.