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]