Dynamic multi-dimensional array in C
Warm and warm
Kefeng@liao.com.cn
[Foreword] The NEW operator of C is a very good syntax feature of the language. However, there are many restrictions on the NEW operator in the actual use. When the prominent point is to allocate a multi-dimensional group space with the New operator, it cannot be made Each dimension of the array is dynamically variable. This article will propose a simple and intuitive solution that describes a simplified model of an actual problem and explains many beginners to the NEW operators in C and the Misunderstanding of the multidimensional array. 1. Problem The proposal - The actual use of multi-dimensional variable groups is the simplified model of problems in actual programming. Chessboard is a chessboard class, where m_board is a two-dimensional array of chess pieces on the board. Dimension is the size or dimension of the board, because it is used for array declarations, so it must be a constant that can determine its value during compilation, where we have used a nameless enumeration. The size of the chessboard for different kinds of chess is different. For black and white chess, Dimension is defined as 8, and the Dimension should be 15, while Go, it has to be 19. This code is compiled with conditions to determine the value of the Dimension constant to ensure that this code has good reusability. Since the M_Board must be a compile period constant, the size of the M_Board array in the program runtime is not changeable. If you want to achieve black and white chess, Wuzi and Go, you can't do this. Okth, this is a bit exaggerated, but even if the light is a game, there are 9x9, 13x13, 19x19 playback board, and should be able to let users freely select.
Class Chessboard {private: enum {#ifdef Othello Dimension = 8 file: // If it is black and white, checkerboard size is 8X8 #ENDIF #ifdef pente dimension = 15 file: // If it is a five-child chess, the board size is 15X15 #ENDIF}; INT M_BOARD [DIMENSION] [DIMENSION]; public: / * Other member functions ... * /} We must use the new operator or Malloc function to dynamically allocate space at the program runtime time, due to New support Multi C features, so our program uses a New operator. 2. Description of the new application multidimensional array in MSDN - further understand the code below the NEW operator is taken from "New Operator" in the MSDN, where the second line is compiled in VC6.0 to get an error message, in this MSDN The description is that the type returned by the New operator is float (*) [25] [10], that is, pointing to the pointer (removed the leftmost one-dimensional) to Float [25] [10]. The correct code should be as shown in 3, 4.
1. Float * fp; 2. fp = new float [10] [25] [10]; // error message: Cannot communication from 'float (*) [25] [10]' to 'float *' 3. Float (* CP) [25] [10]; 4. CP = new float [10] [25] [10]; Refer to this code let us consider our board problem, so that the driver can get the following code:
INT (* m_board); // Declare m_board = new int [change] in the member variable of the class; // According to user selection to determine the corresponding Changeable value is not difficult to see, because it is still necessary to compile Constant Dimension to declare an array, so the M_Board array can only have one-dimensional variable, which is useless to our problem. 3. Solution The two solutions are given here, and the specific code is given to the second solution. 1). We can apply for a single-dimensional array of xsize * ysize, then you can locate the corresponding storage unit by converting the xy subscript, the code is as follows: int * p = new int [= * xsize]; file: // Xsize and ysize should be defined as constant file: //, but the reference to p [y] [x] has a syntax error, should be p [y * xsize x] = y * 1000 x; this method is the largest; The benefit is that the array dimension can be freely determined, and even dynamically determine because it is converted to a one-dimensional array. However, its maximum inconvenience is the cumbersome of the subscript conversion, which is more obvious in the case of multi-dimensional arrays. As described below, the code is a program that the subscript conversion is correct, and its output should be different for each array unit, and all fall between "Start Address" and "End Address".
Const int xsize = 6; const INT xsize = 7; const Int zsize = 9; int * p = new int [ysize * xsize * zsize]; file: // but for P [Y] [x] is commented Grammatical error, should be cout << (int) p << "start address / n"; cout << (int) p) sizeof (int) * ysize * xsize * zsize << "end address / n"; For (int z = 0; z