c++ array相关知识整理

目录

array的initialization

1,Array declaration by specifying the size:

2,Array declaration by initializing elements:

3,Array declaration by specifying the size and initializing elements: 

initialization中的new

与之前的区别:

cppreference中关于new的解释:

关于memory leak:

array与vector

其他


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 pointerstd::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.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值