C ++ two-dimensional array dynamic memory allocation

Source: Internet
Author: User

For memory allocation of two-dimensional arrays and two-dimensional pointers, the first choice here is the memory allocation of one-dimensional pointers and one-dimensional arrays. One-dimensional: array: such as int a [5]. Here, a one-dimensional array a is defined, and the number of elements in the array is 5. Here, a represents the whole of these five elements, that is, we can find these five elements through. Note: a represents the first address of the first element of the array. & A indicates the address of the array, although their values are the same. Pointer: int * p = NULL. Here p is a pointer pointing to a memory block in the computing machine that stores the int type. P = a; that is, let p be equal to the address of the first element of the array just applied. So we can also find the five elements through p, so P [I] works the same as a [I. Note: 1: int * p = NULL; the size of p is 4 on 32-bit machines, even if p = a; then the sizeof (p) of p is still equal to 4. 2: After the declaration, the array must be allocated memory for initialization. In general, pointers are dynamically allocated to memory. 3: Do not confuse pointers and arrays. Pointers are pointers, arrays are arrays, but Arrays can be converted to pointers under certain conditions. Do not confuse pointers with arrays. (For example, the pointer has ++, -- operation, and the array is not allowed ). Dynamic Memory Allocation of one-dimensional pointers: int * p = NULL; p = new int [N]; never forget delete [] p; p = NULL; memory Allocation for two-dimensional arrays: int a [2] [3]; an array of 2X3 = 6 int sizes is allocated here. The second dimension 3 of a two-dimensional array cannot be omitted. The existence of a two-dimensional array in a computer is also a continuous address, but each three elements constitute a one-dimensional array a [I], here, a [I] indicates the address of the first element of the array with the dimension of 3. Therefore, access from a [I] [j] and access from a [I] are clear. Here a [I] is actually the address of the first element of a one-dimensional array. For two-dimensional arrays, we usually use one-dimensional pointers, for example, copying code 1 # include <iostream> 2 void test (int * p) 3 {4 for (int I = 0; I <3; ++ I) 5 {6 for (int j = 0; j <3; ++ j) 7 {8 std: cout <* (p + 3 * I + j); // One-dimensional processing 9} 10} 11} 12 int main (void) 13 {14 int a [3] [3] = {1, 2, 3, 4, 5, 6, 7, 0}; 15 test (int *) ); // treat a two-dimensional array as a one-dimensional processing 16 system ("pause"); 17 return 0; 18} the copied code must be clearly described in the book. C ++ dynamic memory allocation for two-dimensional arrays. Dynamic Array allocation of two-dimensional pointers: Two-Dimensional pointers are similar to int ** p allocation of pointer arrays; copy code 1 # include <iostream> 2 int main (void) 3 {4 int ** p = NULL; // apply for a 3x4 2D array 5 p = new int * [3]; // assign a one-dimensional pointer, assign three int * type one-dimensional pointers. 6 for (int I = 0; I <3; ++ I) 7 {8 p [I] = new int [4]; 9} 10 for (int I = 0; I <3; ++ I) 11 {12 for (int j = 0; j <4; ++ j) 13 {14 p [I] [j] = I * j; 15 std: cout <p [I] [j] <"; 16} 17 std :: cout <std: endl; 18} 19 20 for (int I = 0; I <3; ++ I) // release 21 {22 delete [] p [I]; 23} 24 delete [] p; 25 system ("pause"); 26 return 0; 27} copy the dynamic memory allocation of the Code pointer array the dynamic memory allocation of the pointer array only needs to allocate memory to the array element pointers of the pointer array separately, which is less than the allocation of two-dimensional pointers. Copy code 1 # include <iostream> 2 int main (void) 3 {4 int * a [3]; // apply for an array containing three int * types of pointers 5 // different from the two-dimensional pointer, Here array a does not need to manually apply for memory 6 for (int I = 0; I <3; ++ I) // apply for a 3x4 Space 7 {8 a [I] = new int [4]; 9} 10 for (int I = 0; I <3; ++ I) 11 {12 for (int j = 0; j <4; ++ j) 13 {14 a [I] [j] = I * j; 15 std: cout <a [I] [j] <"; 16} 17 std :: cout <std: endl; 18} 19 20 for (int I = 0; I <3; ++ I) // release three pointer elements, because the array is in the stack area, 21 {22 delete [] a [I]; 2 is automatically released. 3} 24 system ("pause"); 25 return 0; 26} copy the dynamic memory allocation array pointer of the Code array pointer to point to the array pointer. To put it bluntly, it points to an array as a whole, therefore, you can apply for a memory address directly during the allocation. It is similar to static allocation of two-dimensional arrays. Copy code 1 // Karllen 2 int main (void) 3 {4 int (* a) [4]; // here, 4 is the second dimension, the increment base of a is four int5 a = new int [3] [4]; 6 delete [] a; 7 a = NULL; 8 return 0; 9}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.