当前位置:网站首页>Create dynamic array

Create dynamic array

2022-06-25 05:13:00 Ability with desire

  1. explain

There are three important limitations to array type variables : The length of the array is fixed , You must know the array length at compile time , The array is only in the memory of the block statement that defines it .
Each program occupies a block of available memory space during execution , Used to store dynamically allocated objects , This memory space is called the free storage area or heap of the program .
C Language programs use a pair of standard library functions malloc and free Allocate storage space in free memory , and c++ Language uses new and delete Expressions implement the same function .

2. Definition of dynamic array
When dynamically allocating arrays , Just specify the type and array length , You don't have to name the array object ,new Expression returns a pointer to the first element of the newly allocated array :

int *pa = new int[10];// Initialize to 10 Elements 

this new The expression is assigned an expression that contains 10 individual int An array of elements of type , And returns a pointer to the first element of the array , This return value initializes the pointer pia.

3. Initializing dynamically allocated arrays
If the array element has a class type , The class's default constructor will be used to implement initialization , If the array element is a built-in type , There is no initialization :

string *psa = new string[10] ;// Initialize to empty string 
int *pia = new int[10]; // Not initialized 

You can also use a pair of empty parentheses following the length of the array , Initialize the values of array elements . Parentheses require the compiler to initialize the array ,.

int *pia2 = new int[10]();
原网站

版权声明
本文为[Ability with desire]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210522298522.html