Implementing the growth array in C
Growing long-term one-dimensional array
The bearing length array here refers to the array of memory spaces that need to be dynamically allocated at runtime when compiling. The easiest way to achieve a long array is to become a one-dimensional array, you can do this:
// File name: array01.cpp # include
INT main () {int Len; cin >> LEN; // Points the length of the NEW dynamic allocation with a pointer P = new int [LEN]; ...... ..... delete [] p; return 0;}
Note INT * P = new int [LEN]; this sentence, you can't do this: int P [len]; C compiler will report an error in the size of the LEN cannot be sure, because in this form of declaration array, the magnitude of the array needs Determined time compile. And this is not: int p [] = new int [len]; the compiler will say that the int * type is converted to int [] type, because using new memory space, it will return the first address of this memory, So to assign this address to a pointer, so use int * p = new int [len];
Array01.cpp implements a long-long one-dimensional array, but to develop a good habit, pay attention to log out of the pointer P, so that the program releases the memory space opened by New. Of course, the VECTOR in the C Standard Template (STL) can also achieve a growth array:
// File name: array02.cpp # include
INT main () {int Len; cin >> Len; Vector
For (int i = 0; i Here the growth array allows me to think of the ArrayList in the Vector and C # in the Java's java.util package, which can also achieve a growing array in their respective languages. However, the Vector of C cannot recycle the occupied memory space as the hosted garbage collection mechanism like C #, but you can transfer the memory after using the Vector () destructor after using the Vector () destructor. 2. The N-dimensional array of N-dimensional arrays achieved a bit troubles, but in the engineering and software design applications are two-dimensional arrays, it focuses on the growth of two-dimensional arrays, which grows. N-dimensional The array can be implemented in a similar method. First look at an example of a classic use C to become a long-term number of two-dimensional array: // File name: array03.c # include So how do you implement C ? In C , you can dynamically open and release space via New and Delete operators, where NEW is similar to the function of the Malloc function in C, and the DELETE is similar to the function of the Free function in C. Two methods can be employed when achieving a long-length two-dimensional array with C : a two-pointer method and a method of using a Vector (vector) in STL. First introduce the double pointer method, here the double pointer refers to the pointer of the pointer, such as you can declare an array: int ** p = new int * [Num1]; and for each * p (a total of NUM1 * P Apply for a group of memory space: for (int i = 0; i // File Name: Array04.cpp # include INT main () {Int Num1, // Route Num2; // Column number Cout << "please enter the number for row and color: << endl; cin >> Num1 >> Num2; / / Open space int ** p = new int * [Num1] for two-dimensional array; for (int i = 0; I For (int J = 0; J / / Release the space for the two-dimensional array occupies (INT M = 0; M Return 0;} The following is the result of the run: Please enter the number for row and column: 4 5 1: 004915F0 2: 004915F4 3: 004915F8 4: 004915FC 5: 00491600 2: 00491180 4: 00491184 6: 00491188 8: 0049118C 10: 00491190 3: 00491140 6: 00491144 9: 00491148 12: 0049114C 15: 00491150 4: 00491100 8: 00491104 12: 00491108 16: 0049110C 20: 00491110PRESS ANY KEY to Continue Program List Array04.cpp can display the address of the assigned memory space unit, you can see that since the array space is dynamically assigned, the address space between the array lines is discontinuous because the address space of the array element of the unreal is. Use different New to allocate. The address space between the columns in each row is continuous. So how do you realize two-dimensional arrays with a Vector (vector)? The following gives the export program: // File name: array05.cpp #include Return 0;} The following is the result of the run: input value for m, n: 3 4 0: 00491180 0: 00491184 0: 00491188 0: 0049118C 0: 00491140 1: 00491144 2: 00491148 3: 0049114C 0: 00491100 2: 00491104 4: 00491108 6: 0049110CPress any key to continue You can see that the address assignment of the memory in the vector here also has the same feature of the two-dimensional array implemented with the double pointer. However, the use of Vector is more simpler than using the double pointer, and the memory space will be safer. The array initialization code is also simpler, so I recommend using the VECTOR in the STL to achieve a long multi-dimensional array. The following is a three-dimensional array :) // File name: array06.cpp # include COUT << "Input Value for M, N, L:"; CIN >> M >> N >> L; Vector Operation: Input Value for M, N, L: 2 3 4 0: 00492FE0 1: 00492FE4 2: 00492FE8 3: 00492FEC 1: 00492FA0 2: 00492FA4 3: 00492FA8 4: 00492FAC 2: 00492F60 3: 00492F64 4: 00492F68 5: 00492F6C 1: 00492EC0 2: 00492EC4 3: 00492EC8 4: 00492EC 2: 00492E80 3: 00492E84 4: 00492E88 5: 00492E8C 3: 00492E40 4: 00492E44 5: 00492E48 6: 00492E4C Press any key to continche According to a similar approach, you can also use the VECTOR to achieve the growth array of four-dimensional, five-dimensional, or even N-dimensional, but the direct use of the pointer to realize the N-dimensional number of groups will make your head becomes bigger :) (This article has programs to compile operation in VC6)