C two-dimensional array (pointer) dynamic allocation and release is preceded by a clear concept:
The so-called 32-bit processor can only handle 32 bits at a time, that is, 4 bytes of data, and the 64-bit processor can handle 64 bits at a time, that is, 8 bytes of data. If we edit the total length of the 128-bit instruction according to the 16-bit, 32-bit, and 64-bit units: The old 16-bit processor, such as the Intel 80286 CPU requires 8 instructions, 32-bit processors require 4 instructions, and 64-bit processors are only two instructions, obviously, With the same operating frequency, 64-bit processors can be processed faster than 16-bit, 32-bit. In addition to the computational power, the advantages of a 64-bit processor are also reflected in the system's memory control over the 32-bit processor. Since addresses use special integers, an alu (arithmetic logic operator) and register of 64-bit processors can handle larger integers, or larger addresses. Traditional 32-bit processor has a maximum addressing space of 4GB (2 of 32 square = 4294967296bit = 4G or so), making many data processing programs that require large-capacity memory at this time will appear stretched, resulting in a bottleneck of operational efficiency. The 64-bit processor in theory can reach 18 million TB,1TB equals 1024GB,1GB equals 1024MB, so 64-bit processor can completely solve the bottleneck phenomenon of 32-bit computing system, fast people, for those who require multi-processor scalability, 64-bit processors deliver superior performance for larger addressable memory, video/audio/three-dimensional processing, or higher computational accuracy applications.
The 32-bit (bit) and 64-bit system pointers account for different memory, note B differs from B, B is byte (byte), B is bit (bit) 1gb=1024mb,1mb=1024kb,1kb=1024b,1b=8bit
In a 32-bit system, all pointers accounted for 4 bytes. The CPU determines the address of the memory, such as 32-bit CPU has 32 address bus, corresponding to the format of 10 01 .... The system with the 32bit =4byte,32 bit has an addressable capacity of 32 bits, which should be 4 bytes long and the pointer size is 4byte.
64-01 01 10 10 .... The system with the 64bit =8byte,64 bit has an addressable capacity of 64 bits, which should be 8 bytes in length, so the pointer size is 8byte. All of the following are 32-bit system pointers.
(1) Second dimension known
- char (*a) [N]; //Pointer to array
- A = (char (*) [N])malloc (sizeof (char *) * m);
- printf ("%d\n", sizeof (a)); //4, pointer
- printf ("%d\n", sizeof (a[0])); N, one-dimensional array
- Free (a);
(2) The first dimension is known
- char* A[m]; //array of pointers
- int i;
- For (i=0; i<m; i++)
- A[i] = (char *)malloc (sizeof (char) * n);
- printf ("%d\n", sizeof (a)); //4*m, pointer array
- printf ("%d\n", sizeof (a[0])); 4, pointer
- For (i=0; i<m; i++)
- Free (a[i]);
(3) First dimension known, memory allocated at a time (guaranteed memory continuity)
- char* A[m]; //array of pointers
- int i;
- a[0] = (char *)malloc (sizeof (char) * M * n);
- For (i=1; i<m; i++)
- A[i] = a[i-1] + N;
- printf ("%d\n", sizeof (a)); //4*m, pointer array
- printf ("%d\n", sizeof (a[0])); 4, pointer
- Free (a[0]);
(4) Two dimensions are unknown
- Char **a;
- int i;
- A = (char * *)malloc (sizeof (char *) * m); //allocation pointer array
- For (i=0; i<m; i++)
- {
- A[i] = (char *)malloc (sizeof (char) * n); //assigns the array to which each pointer points
- }
- printf ("%d\n", sizeof (a)); //4, pointer
- printf ("%d\n", sizeof (a[0])); //4, pointer
- For (i=0; i<m; i++)
- {
- Free (a[i]);
- }
- Free (a);
(5) Two-dimensional unknown, one-time allocation of memory (guaranteed memory continuity)
- Char **a;
- int i;
- A = (char * *)malloc (sizeof (char *) * m); //allocation pointer array
- a[0] = (char *)malloc (sizeof (char) * m * n); Allocate all space at once
- For (i=1; i<m; i++)
- {
- A[i] = a[i-1] + N;
- }//Using the memory allocation method as above means that the value of a is initialized to the first address of M*n's two-dimensional array, and this block of memory is continuously
- printf ("%d\n", sizeof (a)); //4, pointer
- printf ("%d\n", sizeof (a[0])); //4, pointer
- Free (a[0]);
- Free (a);
Use the (5) method to define **data, allocate m*256 space, and debug as follows:
The conversions are in decimal, respectively:
$1=140737353293840 $8 =6897728
$2=140737353294096 $9 =6897736
$3=140737353294352 $10=6897744
$1,$2,$3 Difference 256
$8,$9,$10 Difference 8 (64-bit system)
2.c++ dynamically allocates a two-dimensional array of 2. C + + dynamic assignment two-dimensional array (1) Known second dimension
- char (*a) [N]; //Pointer to array
- A = new char[m][n];
- printf ("%d\n", sizeof (a)); //4, pointer
- printf ("%d\n", sizeof (a[0])); //n, one-dimensional array
- delete[] A;
(2) The first dimension is known
- char* A[m]; //array of pointers
- For (int i=0; i<m; i++)
- A[i] = new char[n];
- printf ("%d\n", sizeof (a)); //4*m, pointer array
- printf ("%d\n", sizeof (a[0])); //4, pointer
- For (i=0; i<m; i++)
- delete[] a[i];
(3) First dimension known, memory allocated at a time (guaranteed memory continuity)
- char* A[m]; //array of pointers
- a[0] = new char[m*n];
- For (int i=1; i<m; i++)
- A[i] = a[i-1] + N;
- printf ("%d\n", sizeof (a)); //4*m, pointer array
- printf ("%d\n", sizeof (a[0])); //4, pointer
- delete[] a[0];
(4) Two dimensions are unknown
- Char **a;
- A = new char* [M]; //allocation pointer array
- For (int i=0; i<m; i++)
- {
- A[i] = new char[n]; //assigns the array to which each pointer points
- }
- printf ("%d\n", sizeof (a)); //4, pointer
- printf ("%d\n", sizeof (a[0])); //4, pointer
- For (i=0; i<m; i++)
- delete[] a[i];
- delete[] A;
(5) Two-dimensional unknown, one-time allocation of memory (guaranteed memory continuity)
- Char **a;
- A = new char* [m];
- a[0] = new char[m * n]; //Allocate all space at once
- For (int i=1; i<m; i++)
- {
- A[i] = a[i-1] + N; //assigns the array to which each pointer points
- }
- printf ("%d\n", sizeof (a)); //4, pointer
- printf ("%d\n", sizeof (a[0])); //4, pointer
- delete[] a[0];
- delete[] A;
One more word: New and delete to pay attention to pairing, that is, how many new will have the number of delete, so as to avoid memory leaks!
3. Static two-dimensional arrays are passed as function parameters
If the two-dimensional arrays are dynamically allocated using the above methods, then the corresponding data types can be used as function parameters. Here we discuss a static two-dimensional array as a function parameter, which is called as follows:
- int a[2][3];
- Func (a);
In C language, it is troublesome to pass a static two-dimensional array as a parameter, it is usually necessary to indicate the length of the second dimension, if the second dimension is not given, it can only be passed as a one-dimensional pointer, then the linear storage property of the two-dimensional array is transformed into the access to the specified element in the function body. First write the test code to verify the correctness of the parameter passing: (1) Given the second dimension length
- void func(int a[][n])
- {
- printf ("%d\n", a[1][2]);
- }
(2) Not given a second dimensional length
- void func(int* a)
- {
- printf ("%d\n", a[1 * n + 2]); Calculate element position
- }
Note: When using this function, you need to cast the first address of the two-dimensional array to a one-dimensional pointer, func ((int*) a); Note: When using this function, you need to cast the first address of the two-dimensional array to a one-dimensional pointer, func ((int*) a); 8233744
C language two-dimensional array (pointer) dynamic allocation and release (GO)