c++--Programming Common Errors

Source: Internet
Author: User


C + + library functions

C + + standard cubby C standard library to be a lot more complex, we need to study hard. C + + Standard library has been set up late, which solves some problems of C standard library. Through careful study, proficiency will help improve the quality of the code.

Some suggestions:
1. Try to use iterators instead of subscript operations
2. Avoid using pointers and arrays as much as possible using vectors and iterators
3. Replace the C-style string with the string type (using the standard library type string, in addition to enhancing security, the efficiency is improved, so you should try to avoid using C-style strings)
4. When manipulating an array with pointers, you should first choose an iterator-like usage
5. Const when used, if equivalent, try to put the const in the type, to avoid similar to the typedef in the use of errors
6. In general, it is preferable to use standard library abstract classes with fewer low-level arrays and pointers built into the language. In particular, you should use the string type to replace the C-style character array that ends with a null character. Modern C + + programs should not use C-style strings.


Here are some of the errors found in our program:

1. Do not use iterator (in fact, the use of STL);
for (Iter=v.begin; Iter!=v.end (); iter++) {
...
}
if (iter = = V.end ()) {
}
(Missing iter = = V.end () judgment, direct use of *iter. Causes the panic. Anhui and Ningxia have appeared, non-standard wording of the EPG triggered. )

2. Use reinterpret_cast<> conversion when multiple inheritance. (Appears in Hippo. Crash. )

3.override functions are considered to be two different functions when the arguments are inconsistent. (The hippo failed to create the MP object and the video cannot be played.) )


Careless habits, write programs do not pay attention to the environment, context

1.getString only passes the buffer pointer, not the buffer size.
Example: int sysntvuserget (char *buf);
Comments: In our programmer's heart there are always some conventions, to define the program work scenarios. To know that demand will change, memory will be forgotten, hidden in the heart of the agreement is inconvenient to communicate with other colleagues. So keep in mind-do not give the program implicit restrictions, all the constraints must be intuitive, must fall into the code, the text.
Fixed: int sysntvuserget (char *buf, int buflength);

2. Writing procedure is arbitrary, not with the code up and down related thinking
Example 1:if (!strncmp ("PVR"). Schedule.get ", Buff, 15)) {
Do something;
} else if () {
。。。。。。
A large number of code
。。。。。。
} else if (!strncmp ("PVR"). Schedule.getbyid ", Buff, 19)) {
Never do here.
}
Reviews: Do not organize, do not classify, code readability is poor, the problem more hidden deeper, not easy to find

Example 2:char Loguploadinterval_buf[url_len] = {0};
memset (loguploadinterval_buf, 0, Url_len);
sprintf (Loguploadinterval_buf, "%d", XXX);
Comments: A buffer unexpectedly was cleared three times 0, do not know is serious, or careless too much.

Finally, please note: The above mentioned many times "explicit", "intuitive", remember.

c++--Programming Common Errors

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.