1. Today I wrote a program and wrote the following code: char * Parr [10] = new char [20]. Later I found that this writing is wrong. = Char ** type on the left and char * type on the right. Value assignment is not allowed. It cannot be written like this: char * Parr [10] = {New char [20]}; in this way, only the pointer in the first element of the logarithm group initializes the memory area, the other nine elements are not allocated memory. You can enter 10 "New char [20]" in braces and separate them with commas. You can also use the for loop to initialize the 10 elements of the array. In fact, the memory waste is not considered. We can declare a two-dimensional array at the beginning: Char
Arr [10] [20];
2. there are two methods to use the const keyword for pointers. One is to point the pointer to a constant object, such as int age = 3; const int * P = & age; or int const * P = & age; in this case, you cannot use the pointer to modify the value of age, because the pointer points to an int type constant, however, the pointer can be directed to another position. The other is to declare the pointer as a constant, such as int age = 3; int * const P = & age; in this case, the pointer can only point to age, that is, the address pointed to by the pointer cannot be changed, but the value of age can be modified through the pointer.
3. Const and reference parameters. First, two concepts are clarified: The left value parameter is an object that can be referenced, such as a variable, array element, structure member, reference, and unreferenced value. Non-left parameters include literal constants and expressions that contain multiple numbers. Suppose there is such a function, double refcube (double & RA); when the program tries to call a function like refcube (x + 3.0), the compiler will issue a warning, because the expression x + 3.0 is not a variable. If the double refcube (const double & RA) function is declared in this way, C ++ allows refcube (x + 3.0) to call the function, and a temporary variable is generated, and initialize it to the value of the expression x + 3.0, which is a new limitation of C ++. When the referenced parameter is const, C ++ will generate a temporary variable in the following two cases: a) the real parameter type is correct, but not the left value;
B) The real parameter type is incorrect, but can be converted to the correct type. Temporary variables only exist during function calls. So why can I create temporary variables only when const is added, because without const, if the intention of a function that accepts referenced parameters is to modify the variables passed as parameters, creating Temporary variables will block the implementation of this intent. The solution is to prohibit the creation of temporary variables. If the purpose of the refcube () function is only to use passed values instead of modifying them, creating temporary variables will not adversely affect the function, but will make the function more generic in terms of the type of modifiable parameters.
4. strcpy_s () is a function that is safer than strcpy (). Let's talk about the problems I encountered in programming. At that time, when I was running a program with the following constructor, the cause was that the value given by the second parameter was incorrect and should be changed to strlen (s) + 1 (style is the pointer of char ):
HasDma(const char * s){ style = new char[std::strlen(s) + 1]; strcpy_s(style, std::strlen(s), s);}
Let's take a look at the prototype of the three parameter versions:
errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource );
Note that numberofelements here refers to the buffer size.
5. The unique principle of memory alignment: data must be placed in such a memory address, and its memory address is an integer multiple of the type size. For:
struct TEST {int a;char b:1;char c:2;int d;}ts;
Sizeof (TS) = 12. However, note that B and C actually occupy one byte. For more information about bit domains, see the description of bit domains in C language.
6. the virtual function implements late binding. The Destructor are parsed from the outermost layer (late derivation) to the innermost layer (early derivation). If the virtual mechanism is used in the destructor, it is possible to bind to a method that derives from a hierarchy later than the hierarchy to be destructed, and this hierarchy has been destructed, this causes an exception. Therefore, the C ++ compiler ignores the virtual mechanism used in destructor. It means that even if you call other virtual functions in the destructor, you only execute the behaviors in this class and will not bind them to other classes. That is to say, the virtual mechanism is ignored.
7. A better method to calculate the number of elements in an array is as follows:
sizeof(arr)/sizeof(arr[0]);
8. When an array is passed in as a parameter, how can we conveniently know the array size?
One way to pass in the array size as a parameter is to set a special value in the last element of the array, in this way, we can use this special value inside the function to determine whether the value has reached the end of the array.