Note 6: array of compound types

Keywords: C++ less

What is an array

An array is an ordered sequence of elements, a collection used to store data of the same type.

Definition of array

1. Declaration formula (one dimension)

1. Static array

typeName arrayName[arraySize];

2. Dynamic array

typeName* arrayName = new typeName[arraySize];

2. Explanation of array terms

  1. Array name

    There will be a set name containing a limited number of variables of the same type, then the name of this set is array name.

  2. element

    The variables that make up an array are called the elements of the array.

  3. subscript

    The elements of the array are numbered 0 from the first, followed by a sequence of natural numbers. The numeric number of each element of an array is called a subscript.

  4. length

    The number of all elements contained in an array is called the length of the array.

  5. Size

    Element size * array length = array size

  6. precursor

    In an array of length n

    For any 0 ≤ I < J < n, A[i] is the precursor of A[j].

    In particular, for any 1 ≤ I, A[i - 1] is called an intermediate precursor of A[i].

  7. Successor

    In an array of length n

    For any 0 ≤ I < J < n, A[j] is the successor of A[i].

    I n particular, for any I ≤ (n-2), A[i + 1] is called the immediate successor of A[i].

  8. prefix

    All the antecedents of any element form its prefix.

  9. Suffix

    All descendants of any element form its suffix.

  10. Physical address

    For one-dimensional array A[n], each element occupies s units of space, then the physical address of element A[i] is: A + i * s.

Declaration, initialization and release of arrays

1. Statement

1. One dimensional array

  1. Static array

    int s_1dArray[2];

  2. Dynamic array

    int* d_1dArray = new int[2];

2. Two dimensional array

  1. Static array

    int s_2dArray[2][3];

  2. Dynamic array

    int** d_2dArray = new int*[2];
    for(int i = 0; i < 2; i++)
    	d_2dArray[i] = new int[3];
    

3. Three dimensional array

  1. Static array

    int s_3dArray[2][3][4];

  2. Dynamic array

    int*** d_3dArray = new int**[2];
    for(int i = 0; i < 2; i++)
    {
    	d_3dArray[i] = new int*[3];
    	for(int j = 0; j < 3; j++)
    		d_3dArray[i][j] = new int[4];
    }
    

About the statement

  1. Static array

    The value of [] must be known at compile time, that is, it cannot be a variable.

  2. Dynamic array

    The value of [] can be a variable.

  3. special

    The value in [] can be 0.

2. Initialization

1. One dimensional array

  1. Static array

    int s_1dArray_inited[2] = {1,2};

  2. Dynamic array

    int* d_1dArray_inited = new int[2]();

2. Two dimensional array

  1. Static array

    int s_2dArray_inited[2][3] = {{1,2,3},{4,5,6}};

  2. Dynamic array

    int** d_2dArray_inited = new int*[2];
    for(int i = 0; i < 2; i++)
    	d_2dArray_inited[i] = new int[3]();
    

3. Three bit array

  1. Static array

    int s_3dArray_inited[2][3][4] = {{{1,2,3,4},{5,6,7,8},{9,10,11,12}}, {{12,11,10,9},{8,7,6,5},{4,3,2,1}}};

  2. Dynamic array

    int*** d_3dArray_inited = new int**[2];
    for(int i = 0; i < 2; i++)
    {
    	d_3dArray_inited[i] = new int*[3];
    	for(int j = 0; j < 3; j++)
    		d_3dArray_inited[i][j] = new int[4]();
    }
    

About initialization

  1. Static array

    If the initialization is not active, the data in the basic type array will be "dirty data", but the global variable and static variable system will initialize the element to the default value of the specified type. The special class type will call the default constructor, and if the initialization list of the basic type array is empty, the system will initialize the element as the default value of the element.

    When the number of values in the initialized sequence is less than the length of the array, the remaining elements are automatically initialized to the default value.

  2. Dynamic array

    If the initialization is not active, the data in the array is "dirty data".

3. Release

1. One dimensional array

Dynamic array

delete[] s_1dArray;

2. Two dimensional array

Dynamic array

for(int i = 0; i < 2; i++)
		delete[] d_2dArray[i];

3. Three dimensional array

Dynamic array

	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 3; j++)
			delete[] d_3dArray[i][j];
	}

About release

  1. Static array

    The static array is allocated in the stack of memory. After the function is executed, the static array will be destroyed. This is done automatically by the system.

  2. Dynamic array

    The memory allocated dynamically is on the heap, so the system cannot release the memory on the heap automatically, so it needs to release it actively.

Examples of using arrays

#include<iostream>

using namespace std;


int main()
{
	int s_1dArray[2];
	int* d_1dArray = new int[2];
	int s_2dArray[2][3];
	int** d_2dArray = new int*[2];
	for(int i = 0; i < 2; i++)
		d_2dArray[i] = new int[3];
	int* s_3dArray[2][3][4];
	int*** d_3dArray = new int**[2];
	for(int i = 0; i < 2; i++)
	{
		d_3dArray[i] = new int*[3];
		for(int j = 0; j < 3; j++)
			d_3dArray[i][j] = new int[4];
	}
	
	delete[] s_1dArray;
	for(int i = 0; i < 2; i++)
		delete[] d_2dArray[i];
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 3; j++)
			delete[] d_3dArray[i][j];
	}
	
	
	int s_1dArray_inited[2] = {1,2};
	int* d_1dArray_inited = new int[2]();
	int s_2dArray_inited[2][3] = {{1,2,3},{4,5,6}};
	int** d_2dArray_inited = new int*[2];
	for(int i = 0; i < 2; i++)
		d_2dArray_inited[i] = new int[3]();
	int s_3dArray_inited[2][3][4] = {{{1,2,3,4},{5,6,7,8},{9,10,11,12}},
									  {{12,11,10,9},{8,7,6,5},{4,3,2,1}}};
	int*** d_3dArray_inited = new int**[2];
	for(int i = 0; i < 2; i++)
	{
		d_3dArray_inited[i] = new int*[3];
		for(int j = 0; j < 3; j++)
			d_3dArray_inited[i][j] = new int[4]();
	}
	
	for(int i = 0; i < 2; i++)
		cout << s_1dArray_inited[i] << " ";
	cout << endl << endl;
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 3; j++)
			cout << s_2dArray_inited[i][j] << " ";
		cout << endl;
	}
	cout << endl;
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			for(int k = 0; k < 4; k++)
				cout << s_3dArray_inited[i][j][k] << " ";
			cout << endl;
		}
	}
    return 0;
}
/* The operation result is: 
1 2

1 2 3
4 5 6

1 2 3 4
5 6 7 8
9 10 11 12
12 11 10 9
8 7 6 5
4 3 2 1

--------------------------------
Process exited after 0.5921 seconds with return value 0
 Please press any key to continue
*/ 

Posted by SlimSlyk on Fri, 24 Apr 2020 09:41:27 -0700