Dynamic Allocation and release of two-dimensional arrays (pointers) in C Language

Source: Internet
Author: User
C Two-dimensional array (pointer) Dynamic Allocation and release first clarify the concept:
The 32-bit processor can only process 32-bit data at a time, that is, 4 bytes of data. The 64-bit processor can process 64-bit data at a time, that is, 8 bytes of data. If we edit the 128-bit command in units of 16-bit, 32-bit, and 64-bit, the old 16-bit processor, for example, the intel 80286 CPU needs 8 commands, A 32-bit processor requires four commands, while a 64-bit processor requires only two commands. Obviously, when the operating frequency is the same, the processing speed of a 64-bit processor is faster than that of a 16-bit or 32-bit processor. In addition to the computing power, compared with 32-bit processors, the advantage of 64-bit processors is also reflected in the system's memory control. Because the address uses a special integer, a 64-bit processor's Alu (arithmetic logic processor) and register can process larger integers, that is, a larger address. The addressing space of traditional 32-bit processors is up to 4 GB (the power of 2 is about 4294967296bit = 4 GB), which makes many data processing programs that require large memory capacity too slow, the bottleneck of operation efficiency is formed. In theory, 64-bit processors can reach 18 million TB, 1 TB equals 1024 GB, and 1 GB equals 1024 MB, therefore, the 64-bit processor can completely solve the bottleneck encountered by 32-bit computing systems, which is faster than humans, for applications that require multi-processor scalability, greater addressable memory, video/audio/3D processing, or higher computing accuracy, a 64-bit processor delivers superior performance.

32-bit (BIT) and 64-bit (BIT) system pointers occupy different memory. Note that B is different from B, B is byte (bytes), and B is bit) 1 GB = 1024 MB, 1 MB = 1024kb, 1kb = 1024b, 1B = 8bit

In a 32-bit system, all pointers occupy 4 bytes. The CPU determines the address of the memory. For example, if the 32-bit CPU has 32 address buses, the corresponding address format is 10 01 .... 01 01 = 32bit = 4 byte. The addressing capacity of a 32-bit system is 32 binary bits, which should be 4 bytes in length and the pointer size is 4 bytes.

64-> 01 01 10 10 .... 01 = 64bit = 8 byte. The addressing capacity of a 64-bit system is 64 binary bits. It should be 8 bytes in length, so the pointer size is 8 bytes. The following are all 32-bit system pointers.

(1) The second dimension is known.
Char (* A) [N]; // pointer to the 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 ();
(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) know the first dimension and allocate memory at a time (to ensure memory continuity)

 

Char * A [m]; // array int I of the pointer; 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) unknown for both dimensions
Char ** A; int I; A = (char **) malloc (sizeof (char *) * m); // assign a pointer array for (I = 0; I <m; I ++) {A [I] = (char *) malloc (sizeof (char) * n ); // allocate the array pointed to by each pointer} 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 ();
(5) unknown in both dimensions, one memory allocation (ensure memory continuity)
Char ** A; int I; A = (char **) malloc (sizeof (char *) * m); // assign the pointer array a [0] = (char *) malloc (sizeof (char) * m * n); // allocate all space at a time for (I = 1; I <m; I ++) {A [I] = A [I-1] + N;} // use the above memory allocation method, which means that the value of a is initialized to the first address of the Two-dimensional array M * n, and this memory is continuously printf ("% d \ n", sizeof (a); // 4, pointer printf ("% d \ n ", sizeof (A [0]); // 4, pointer free (A [0]); free ();
Use the (5) method to define ** data and allocate M * 256 space. The debugging is as follows:

Convert to decimal:

$1 = 140737353293840 $8 = 6897728
$2 = 140737353294096 $9 = 6897736
$3 = 140737353294352 $10 = 6897744

$1, $2, and $3 differ by 256

$8, $9, $10 (64-bit System)

2. c ++ dynamically allocates two-dimensional arrays2. c ++ Dynamic Allocation of two-dimensional arrays (1) known 2D
Char (* A) [N]; // pointer to the array A = new char [m] [N]; printf ("% d \ n ", sizeof (a); // 4, pointer printf ("% d \ n", sizeof (A [0]); // n, one-dimensional array Delete [];
(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) know the first dimension and allocate memory at a time (to ensure 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) unknown for both dimensions
Char ** A; A = new char * [m]; // assign a pointer array for (INT I = 0; I <m; I ++) {A [I] = new char [N]; // assign the array pointed to by each pointer} printf ("% d \ n", sizeof ()); // 4, pointer printf ("% d \ n", sizeof (A [0]); // 4, pointer for (I = 0; I <m; I ++) Delete [] a [I]; Delete [];

 

(5) unknown in both dimensions, one memory allocation (ensure memory continuity)
Char ** A; A = new char * [m]; A [0] = new char [M * n]; // allocate all space at a time for (INT I = 1; I <m; I ++) {A [I] = A [I-1] + N; // assign the array pointed to by each pointer} printf ("% d \ n ", sizeof (a); // 4, pointer printf ("% d \ n", sizeof (A [0]); // 4, pointer Delete [] a [0]; Delete [];

To avoid Memory leakage, pay attention to the pairing of new and delete, that is, the number of new and delete operations!

 

3. The static two-dimensional array is passed as the function parameter.

If the preceding methods are used to dynamically allocate two-dimensional arrays, you can use the corresponding data type as a function parameter. Here we will discuss the transmission of Static Two-dimensional arrays as function parameters, that is, according to the following call method:

int a[2][3]; func(a);

 

In C language, it is difficult to pass static two-dimensional arrays as parameters. Generally, the length of the second-dimensional array must be specified. If the second-dimensional length is not specified, it can only be passed as a one-dimensional pointer first, then, the linear Storage Feature of the Two-dimensional array is used to convert the function into access to the specified element. First, write the test code to verify the correctness of the parameter transfer: (1) given the second-dimensional length
void func(int a[][N]) {     printf("%d\n", a[1][2]); }
(2) The second-dimensional length is not given.
Void func (int * A) {printf ("% d \ n", a [1 * n + 2]); // calculates the element position}
Note: When using this function, you must forcibly convert the first address of a two-dimensional array to a one-dimensional pointer, that is, func (int *) a). Note: when using this function, the first address of the Two-dimensional array must be forcibly converted to a one-dimensional pointer, that is, func (int *) );

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.