目录
1,Array declaration by specifying the size:
2,Array declaration by initializing elements:
3,Array declaration by specifying the size and initializing elements:
array的initialization
1,Array declaration by specifying the size:
int a[10];
2,Array declaration by initializing elements:
int a[] = {1, 2, 3};
3,Array declaration by specifying the size and initializing elements:
int arr[6] = { 10, 20, 30, 40 };
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
// 0. above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
initialization中的new
int* a = new int[3];
PS: initializer list:
int* a = new int[10] { 1,2,3,4,5,6,7,8,9,10 };
与之前的区别:
【无new】Declares a as an array of int containing 10 elements.If declared at file scope, this array will typically reside in the data segment, while if it is declared at block scope it will typically reside on the stack. Depending on context, it has automatic or static storage. The size can only be compile time constant. The array is destroyed and deallocated automatically.
【new】Declares a as a pointer to int, and initializes it with a pointer to dynamically allocated memory which points to the first member of a 10 member array of int. This dynamically allocated memory typically resides on the heap. The size of dynamic array can be determined at runtime. The array is not destroyed and deallocated automatically. If not deallocated, the memory will leak.
总结:
- 无new:是array;编译时就要指定好size;无需手动释放内存。
- new:是pointer;dynamic size,即size中可以有变量;必须手动释放内存。
cppreference中关于new的解释:
Creates and initializes objects with dynamic storage duration, that is, objects whose lifetime is not necessarily limited by the scope in which they were created.
The new expression attempts to allocate storage and then attempts to construct and initialize either a single unnamed object, or an unnamed array of objects in the allocated storage. The new-expression returns a prvalue pointer to the constructed object or, if an array of objects was constructed, a pointer to the initial element of the array.
关于memory leak:
The objects created by new-expressions (objects with dynamic storage duration) persist until the pointer returned by the new-expression is used in a matching delete-expression. If the original value of pointer is lost, the object becomes unreachable and cannot be deallocated: a memory leak occurs.
int* p = new int(7); // dynamically allocated int with value 7 p = nullptr; // memory leak //the pointer goes out of scope: void f() { int* p = new int(7); } // memory leak //exceptions: void f() { int* p = new int(7); g(); // may throw delete p; // okay if no exception } // memory leak if g() throws
To simplify management of dynamically-allocated objects, the result of a new-expression is often stored in a smart pointer: std::auto_ptr (until C++17)std::unique_ptr, or std::shared_ptr (since C++11). These pointers guarantee that the delete expression is executed in the situations shown above.
it will include every token that can be a part of a declarator:
new int + 1; // okay: parsed as (new int) + 1, increments a pointer returned by new int new int * 1; // error: parsed as (new int*) (1)
array与vector
其他
No Index Out of bound Checking:
There is no index out of bounds checking in C/C++, for example, the following program compiles fine but may produce unexpected output when run.