[C++のRelated Concepts] The difference between static array [] and dynamic array new

Reference:
The difference between C++ static array ([]) and dynamic array (new)
C++ reference to array example analysis

storage and operation mode new

new allocates space on the heap, which is inefficient and requires you to delete;
[] is directly allocated on the stack and will be automatically released, which is efficient, but the stack space is limited.

Use sizeof operation

int a[5]; 
sizeof(&a)=4;
//int a[5];在编译阶段分配空间
//sizeof(a);同样在编译阶段执行
sizeof(a)=5*4;
sizeof(*a)=4;
//因为整个数组共占20字节
//首个元素(int型)占4字节。

//同理
int a[4][5];
sizeof(&a)=4;
sizeof(a)=4*5*4=80;
sizeof(a[0])=5*4=20;
sizeof(a[0][0])=4;

int *a=new int[4];
//int *a=new int[4];在运行分配空间
//sizeof(a);只在编译阶段执行,所以只能得到指针大小为4
sizeof(a)=sizeof(*a)=4
//因为地址位数为4字节
//int型也占4字节。

as a function parameter

Array references check the array dimension

//正确
int  a[10] ;  
int  (&b)[10] = a ;

//错误:cannot convert from 'int [10]' to 'int *&'。
int  a[10] ;
int* &b = a ;

Passing an array as a parameter will perform "array markdown"
, that is, the compiler will not check the dimension of the array

#include <iostream>
//void test( char arr[100] ) 等同于 void test( char arr[] )
//也等同于void test( char* const arr )
//所以你甚至可以往数组内传递arr[20]
void test( char arr[100] )
{
    
    
   std::cout << sizeof(arr) << std::endl ; // 输出 4
}
void main()
{
    
    
  char arr[100] = {
    
     0 };
  std::cout << sizeof(arr) << std::endl; // 输出 100 
  test( arr );
}

Method 1 to avoid "array markdown" caused by parameter passing:

......
//使用常量引用数组,但这样数组长度就只能固定不动了
void test( const char (&arr)[100] )
{
    
    
   std::cout << sizeof(arr) << std::endl ; // 输出 100
}
......
  char arr[100] = {
    
     0 };
  std::cout << sizeof(arr) << std::endl; // 输出 100 
  test( arr );
......

//错误!
char arr[20] = {
    
    0};
test( arr ) ;

Method 2 to avoid "array markdown" caused by parameter passing:

//虽然使用模板可以使数组长度可变,但是有以下问题:
//1.当有多个不同的test调用时,会产生多份test代码。
//  而传统的函数调用只有一份代,也调用的次数无关。
//2.不能传入动态分配的指针变量,因为模板函数在编译阶段
//  就需要确定数组的大小,从而生成代码,而指针变量
//  是在运行阶段才得以执行
template <int sz>
void test(char (&arr)[sz])
{
    
    
  for ( int i = 0; i < sz; i++ )
......}
char a[2] = {
    
     0 }, b[15] = {
    
     0 };
test(a);  //正确
test(b);  //正确

as function return value

The static array declared by the function cannot be returned by the function, because of the problem of lifetime, the memory occupied by the internal variables of the function is released after the function is called. If you want to return an array through a function, you can dynamically create the array with new in the function, and then return its first address.
The reason can be understood in this way, because the [] static array is applied on the stack, and the local variables in the function are also on the stack, and the new dynamic array is allocated on the heap, so after the function returns, the Things are automatically released, and things in the heap are not automatically released if there is no delete.

//正确
int* test(){
    
    
	int* b = new int[5];
	return b;
}

//错误
int* test(){
    
    
	int b[5];
	return b;
}

Guess you like

Origin blog.csdn.net/weixin_45005811/article/details/114828494