Discussion of multidimensional, dynamic array storage and access
First, first see a piece of code allocated by one of the following multi-dimensional array
#include
Using namespace std;
Int main (int Argc, char * argv [])
{
Char sz_temp [3] [3]; // Assign static two-dimensional array
Char (* SZ) [3] = null; // Pointer initialization
SZ = (CHAR (*) [3]) SZ_TEMP; // Declare a pointer to the one-dimensional array, mainly used to access rows in the 2nd dimensional array
STRCPY (* (SZ 1), "ABC"); // Copy data
Return 0;
}
discuss:
For some of the code, someone will say, this is so simple, the old Tan's book is this, 嘿嘿 ~, it is really simple, here is just a comparison.
Next, let's take a look at its address map:
We can see that it can be accessed in accordance with dimension (Example: SZ [2] [2], SZ [1] [3]); but we can also use SZ [0] [1], SZ [ 0] [3], SZ [0] [5] .... Or until SZ [0] [9], in the address we can see that their storage procedures are continuous: stored in the order of elements . Continuous spatial allocation from 0 to 9.
Second, now let's take a look at a code about the multi-dimensional dynamic array assignment.
#include
Using namespace std;
Int main (int Argc, char * argv [])
{
INT count = 5;
Char ** sz_temp = (char **) Malloc (count) * sizeof (char *));
CHAR (* SZ) [20] = NULL;
For (int idx = 0; idx { SZ_TEMP [IDX] = (char *) Malloc (20 * sizeof (char)); } SZ = (CHAR (*) [20]) SZ_TEMP; // converts SZ_TEMP into a pointer to the array of 20 CHAR elements. STRCPY (* (SZ 1), "ABC"); // Error, you can think about why it is wrong? Free (sz); Return 0; } discuss: First look at the following address map: (This figure is actually illustrated by everything) SZ is now converted, although it starts at the beginning of the address or the address of SZ_TEMP, but SZ has regarded this dynamic allocation of the array as an array of char, that is, in the eyes, SZ_TEMP allocation space is full. The space of the char element, and it moves the size of 20 char's addresses (because the type is (char (*) [20])), again, the SZ access SZ_TEMP will never show the characteristics of the pointer, it Just CHAR type data (replaced the storage of the pointer in char), then because 1 char is 1 bytes, the size of 1 pointer is 4 Bytes, so: SZ 1 => SZ_TEMP 5 / / 20 * 1 and 5 * 4 Therefore, you can see the address of SZ 1 and the SZ_TEMP 5 address is the same address, strcpy (* (SZ 1), "ABC") It writes SZ 1 after the ABC, can be seen, & sz [0] [4] corresponds to SZ_TEMP 1, and the same address is also the same. Some people may ask: "SZ = (CHAR (*) [20]) SZ_TEMP is used in static allocation and dynamic allocation;" Why will it be different? The reason is as follows: 1. For SZ = (char (*) [20]) SZ_TEMP; SZ_TEMP itself is the first line of the two-dimensional array, just like a one-dimensional array, point to the first element is one thing, if we Put the SZ = (char (*) [20]) sz_temp in the first example; change to SZ = SZ_TEMP; then it is also correct, SZ_TEMP format itself is (char (*) [20]) type, There is no need to convert. 2. For SZ = (char (*) [20]) SZ_TEMP; because SZ_TEMP is not a two-dimensional array of group names, it is a pointer, so there is no one in Article 1, can represent The line in the two-dimensional array, therefore, when converted, SZ will put (char (*) [20]) SZ_TEMP; the dynamic array refers to a row in a two-dimensional array, when SZ is accessible to the dynamic array When the SZ_TEMP assigns this element of this dynamic array, all the elements in the dynamic array see the CHAR type data, so the address of the SZ 1 is the same address with the SZ_TEMP 5 address. 3. Then, the array name address in the static array is the address of the address and the first element of the first line and the first element; the pointer address pointing to the dynamic array is not the address of the dynamic array, this must be cleared. Hey, the source of this problem is the problem of a netizen on the 9CBS. Now, look at the problem solve ~ #include Using namespace std; Typedef char (* charrrp) [20]; Void Fun (Chararrp ArrayList) { STRCPY (* (arraylist), "abc"); // ok, IT IS Right. } Int main (int Argc, char * argv []) { INT count = 5; Char ** sz_temp = (char **) Malloc (count) * sizeof (char *)); Chararrp Sz = NULL; For (int idx = 0; idx { SZ_TEMP [IDX] = (char *) Malloc (20 * sizeof (char)); } For (int i = 0; i { SZ = (Chararrp) SZ_TEMP [i]; Fun (SZ); / * 1. SZ = (charrp) SZ_TEMP [i]; it is converted to (* Charrrp) [20] in SZ_TEMP, to point to a one-dimensional array containing 20 elements. In this way, FUN (SZ) can be called. 2. Since the dynamic assignment of 20 elements is a set of 5-segment memory, the segment is continuous, and the section is not continuous, so it is not accessible with SZ N, SZ_TEMP [IDX] = (char *) Malloc (20 * Sizeof (char)); its assignment is not followed by & SZ_TEMP [IDX-1] [19]. * / } Free (sz); Return 0; }