Array and pointer, array pointer
1. dynamically allocate a one-dimensional array
Int * p = (int *) malloc (sizeof (int) * 10); // or int * p = new int [10];
2. dynamically allocate two-dimensional arrays 2X5
Int * p = (int *) malloc (sizeof (int) * 10); // or int * p = new int [10]; int (* pp) [5] = (int (*) [5]) p;
3. dynamically allocate a 3D array 12 = 3 (surface) x 2 (ROW) x 2 (column)
Int * p = (int *) malloc (sizeof (int) * 12); // or int * p = new int [12] int (* pp) [2] [2] = (int (*) [2] [2]) p;
Consider the memory allocation of the pointer array below.
A. One-dimensional pointer Array
1. array on stack for (int I = 0; I <10; I ++) {B [I] = new int;} for (int j = 0; j <10; j ++) {delete B [j];} // 2. int ** p = new int * [10]; // The array cannot be written as new (int *) [10]; for (int I = 0; I <10; I ++) {B [I] = new int;} for (int j = 0; j <10; j ++) {delete B [j];} delete [] p; // important
B. Two-Dimensional pointer Array
Int * B [4] [5]; // 10 pointer arrays with the content as pointers. // 1. array on Stack // 2. int ** p = new int * [20]; int * (* pp) [5] = (int * (*) [5]) p; // pointer to the two-dimensional pointer Array
The 32-bit pointer is always 4 bytes. the pointer type indicates the way variables are read.