Some details about C ++ [3: Composite type]

xiaoxiao2021-03-05  23

4. Composite type

4.1 The compiler does not check if the array subscript is valid.

4.2 Strings such as: "AVD" or "s" its initial actual represents the memory address where the string is located

4.3 C common pointers rather than arrays to process strings.

EG: (CIN >> Year) .GET (); // CIN Removes '/ n' in the input queue, you need to discard it with no ginseng GET () so that it will not be used next time.

4.4 Dark Question:

In the previous C version GetLine (), get (), when you read the space, start reading at the end of the previous statement

Current Practice: When you get () [non-getLine ()] After reading the space line, the Failbit will be set, the next input will be blocked, you need to recover your input with cin.clear ().

4.5 If the input string is longer than the allocation space, getLine (), get () leaves the remaining characters in the input queue, and getLine () also sets the outage.

4.6 Structure C declares that the keyword struct; C language must be added.

4.7 Some C old versions cannot be initialized to regular arrays, structures, and the keyword Static declaration is static variable, thereby initialization. // Static variable must be initialized.

4.8 C does not advocate the use of external variables, but advocates the use of external structural statements.

4.9 Separate structural definitions and variable declarations to make the program easy to read.

4.10 can declare that there is no name, but must be declared at the same time.

EG: struct {int x; int y;} Point1, Point2;

4.11 Common Body Union, its member will be in the same address, only one is activity. You can also be anonymous.

4.12 Enumerate Enum Creating a symbol constant, can replace const.

* Value range of enumeration type: All positive, the minimum value is 0, the maximum value is not less than the maximum, the multiplication is reduced; when there is a negative number, the minimum value is not less than the negative number The absolute value of the minimum 2 is reduced to the opposite side. // If the maximum is 101, the maximum value is 127.

* Only assignment operation, no calculation operation.

* Enumeration is integer, can be upgraded to int, but int cannot be automatically converted to an enumeration type; if the int value is valid [ie: In the enumeration range], you can force the convert to an enumeration type. If it is out, The results are uncertain, not wrong, but cannot rely on the results.

* You can create multiple values ​​of the same symbol constants.

* Allow anonymous.

4.13 pointer

* Release reference to the pointer means getting the value indicated by the pointer.

* Indirect value or contact reference operator '*', its left and right spaces can be selected: int * ptr; // c-style int * PTR; // C style.

* Pointer initialization, initialize the address, not the value.

* C When you create a pointer, only the memory used to store the address is allocated, without assigning the memory of the pointer pointing value.

* After the number (integer) is forced to convert to the address type, the value can be assigned: int * pt = (int *) 0xB8000000;

4.14 New Typename PNAME = New TypeName

* The use of the pointer, allocates unnamed memory to store the value during the run phase.

* New Failure returns 0 and may trigger Bad_alloc abnormalities.

** DELETE can only be used to release memory allocated with NEW, which cannot be released for memory allocated with a declaration.

** Do not create two pointers that point to the same memory block, because DELETE may appear twice the same memory block, and this is not allowed.

* Delete PNAME; the memory to which PNAME points to, but does not delete the PNAME itself, so PNAME can also be used to point to other memory blocks.

* Create / remove dynamic array EG: int * ps = new int [20]; / delete [] ps; // cannot be released by delete ps;

* C resolves the array name to the address, number of group names, and pointer basic equivalents. Just: pointer can be modified, and the array name cannot be modified.

* If you use new [] to allocate memory, apply DELETE to release. 4.15 When pointing to the string constant with a char pointer (char *), pointing to the address of the string, use const to modify, to ensure that the CHAR pointer cannot be modified String constance.

4.16 Provides a pointer to COUT, which will print its address; if char * is provided for COUT, the string is displayed. To display its address, you need to use the (int *) PS to force the conversion pointer to the address type.

4.17 New delete manages a memory pool, with local variables (automatic variables), static storage memory is separated, so its data life is not controlled by the survival time of the program or function. If the memory is allocated, but it will result in Other programs cannot use the memory block to leak memory.

Welcome all exchanges and fingers,

转载请注明原文地址:https://www.9cbs.com/read-34772.html

New Post(0)