[c/c++]_[Primary]_[programming error-prone place]

Source: Internet
Author: User


Scene:

1. Here is a summary of some of the daily error-prone details.


Issue 1: A Class A has a member variable int deleted, given an object pointer of a *a, to determine deleted is true when the output of a statement.

In general, the Novice will write this:

if (a) {if (a->deleted)       {           cout << "deleted" << Endl;       }}

But this is not enough to streamline and waste the number of lines, it should be.

if (a && a->deleted) {    cout << "deleted" << Endl;}


Issue 2: Create a path string for tchar* (wchar_t*).

Note: The size of malloc out is not the same as the size of the array declaration, do you think the following storage path is large enough?

wchar_t* info = (wchar_t*) malloc (MAX_PATH);


This defines the size of only Max_path bytes and cannot store max_path wide bytes. To define this to support MAX_PATH-size wide-byte, the following is the same:

#include <iostream> #include <stdlib.h>using namespace std;int main (int argc, char const *argv[]) {wchar_t* info = (wchar_t*) malloc (sizeof (wchar_t) *max_path) std::cout << "sizeof (wchar_t) *max_path:" << sizeof ( wchar_t) *max_path << std::endl;wchar_t info2[max_path];std::cout << "Info2:" << sizeof (Info2) < < Std::endl;return 0;}


Output:

sizeof (wchar_t) *max_path:520info2:520


Question 3: Mistakenly assume that pointer +1 is the address value +1

The rule for pointer p+n is (char*) p + sizeof (*P) *n, that is, the size of the P pointer type determines the increment in multiples.

#include <iostream> #include <stdlib.h>using namespace std;int main (int argc, char const *argv[]) {wchar_t* info = (wchar_t*) malloc (sizeof (wchar_t) *max_path) std::cout << "sizeof (wchar_t) *max_path:" << sizeof ( wchar_t) *max_path << std::endl;wchar_t info2[max_path];std::cout << "Info2:" << sizeof (Info2) < < Std::endl;std::cout << (int*) Info << ":" << (int*) (info+1) << Std::endl; Std::cout << (int*) ((char*) (info) +sizeof (*info) *) << ":" << (int*) (info+1) << Std::endl; return 0;}

Output:

sizeof (wchar_t) *MAX_PATH:520INFO2:5200X506CC0:0X506CC20X506CC2:0X506CC2





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

[c/c++]_[Primary]_[programming error-prone place]

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.