C-language two-dimensional array (pointer) dynamic allocation and release

Source: Internet
Author: User

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
    1. char (*a) [N]; //Pointer to array
    2. A = (char (*) [N])malloc (sizeof (char *) * m);
    3. printf ("%d\n", sizeof (a)); //4, pointer
    4. printf ("%d\n", sizeof (a[0])); N, one-dimensional array
    5. Free (a);

(2) The first dimension is known
  1. char* A[m]; //array of pointers
  2. int i;
  3. For (i=0; i<m; i++)
  4. A[i] = (char *)malloc (sizeof (char) * n);
  5. printf ("%d\n", sizeof (a)); //4*m, pointer array
  6. printf ("%d\n", sizeof (a[0])); 4, pointer
  7. For (i=0; i<m; i++)
  8. Free (a[i]);

(3) First dimension known, memory allocated at a time (guaranteed memory continuity)

  1. char* A[m]; //array of pointers
  2. int i;
  3. a[0] = (char *)malloc (sizeof (char) * M * n);
  4. For (i=1; i<m; i++)
  5. A[i] = a[i-1] + N;
  6. printf ("%d\n", sizeof (a)); //4*m, pointer array
  7. printf ("%d\n", sizeof (a[0])); 4, pointer
  8. Free (a[0]);

(4) Two dimensions are unknown
  1. Char **a;
  2. int i;
  3. A = (char * *)malloc (sizeof (char *) * m); //allocation pointer array
  4. For (i=0; i<m; i++)
  5. {
  6. A[i] = (char *)malloc (sizeof (char) * n); //assigns the array to which each pointer points
  7. }
  8. printf ("%d\n", sizeof (a)); //4, pointer
  9. printf ("%d\n", sizeof (a[0])); //4, pointer
  10. For (i=0; i<m; i++)
  11. {
  12. Free (a[i]);
  13. }
  14. Free (a);

(5) Two-dimensional unknown, one-time allocation of memory (guaranteed memory continuity)
  1. Char **a;
  2. int i;
  3. A = (char * *)malloc (sizeof (char *) * m); //allocation pointer array
  4. a[0] = (char *)malloc (sizeof (char) * m * n); Allocate all space at once
  5. For (i=1; i<m; i++)
  6. {
  7. A[i] = a[i-1] + N;
  8. }//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
  9. printf ("%d\n", sizeof (a)); //4, pointer
  10. printf ("%d\n", sizeof (a[0])); //4, pointer
  11. Free (a[0]);
  12. 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
    1. char (*a) [N]; //Pointer to array
    2. A = new char[m][n];
    3. printf ("%d\n", sizeof (a)); //4, pointer
    4. printf ("%d\n", sizeof (a[0])); //n, one-dimensional array
    5. delete[] A;

(2) The first dimension is known
  1. char* A[m]; //array of pointers
  2. For (int i=0; i<m; i++)
  3. A[i] = new char[n];
  4. printf ("%d\n", sizeof (a)); //4*m, pointer array
  5. printf ("%d\n", sizeof (a[0])); //4, pointer
  6. For (i=0; i<m; i++)
  7. delete[] a[i];


(3) First dimension known, memory allocated at a time (guaranteed memory continuity)
  1. char* A[m]; //array of pointers
  2. a[0] = new char[m*n];
  3. For (int i=1; i<m; i++)
  4. A[i] = a[i-1] + N;
  5. printf ("%d\n", sizeof (a)); //4*m, pointer array
  6. printf ("%d\n", sizeof (a[0])); //4, pointer
  7. delete[] a[0];

(4) Two dimensions are unknown
  1. Char **a;
  2. A = new char* [M]; //allocation pointer array
  3. For (int i=0; i<m; i++)
  4. {
  5. A[i] = new char[n]; //assigns the array to which each pointer points
  6. }
  7. printf ("%d\n", sizeof (a)); //4, pointer
  8. printf ("%d\n", sizeof (a[0])); //4, pointer
  9. For (i=0; i<m; i++)
  10. delete[] a[i];
  11. delete[] A;

(5) Two-dimensional unknown, one-time allocation of memory (guaranteed memory continuity)
  1. Char **a;
  2. A = new char* [m];
  3. a[0] = new char[m * n]; //Allocate all space at once
  4. For (int i=1; i<m; i++)
  5. {
  6. A[i] = a[i-1] + N; //assigns the array to which each pointer points
  7. }
  8. printf ("%d\n", sizeof (a)); //4, pointer
  9. printf ("%d\n", sizeof (a[0])); //4, pointer
  10. delete[] a[0];
  11. 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:

    1. int a[2][3];
    2. 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
    1. void func(int a[][n])
    2. {
    3. printf ("%d\n", a[1][2]);
    4. }
(2) Not given a second dimensional length
    1. void func(int* a)
    2. {
    3. printf ("%d\n", a[1 * n + 2]); Calculate element position
    4. }

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)

Related Article

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.