The two-dimensional array is broken!

Source: Internet
Author: User

We are constantly faced with the problem of two-dimensional arrays, and we are always afraid of them. The pointers are really annoying, and the memory is even more annoying.

Summary today: for future reference.
Write only the most commonly used
1: allocate int A [m] [N] on the stack.
Disadvantage, m, n must be known
Function parameter call
Fun (int A [] [N], int dims_ I, int dims_j)
{
A [I] [J] = 0;
}

2: Dynamic Allocation-use STL
Highly recommended !!! Although this method may cause memory disconnections, it is more intuitive to call than the code, and you do not need to manage the memory yourself. The disadvantages are negligible !!!

  1. Int M = 3;
  2. Int n = 3;
  3. Vector <vector <int> myvec (M, vector <int> (n ));
  4. For (INT I = 0; I <m; ++ I)
  5. {
  6. For (Int J = 0; j <n; ++ J)
  7. {
  8. Myvec [I] [J] = I * J;
  9. }
  10. }

  1. Vector <vector <int>;
  2. A. Resize (m); // m rows
  3. For (INT I = 1; I <n; I ++ )//
  4. {
  5. Ga [I]. Resize (n); // n Column
  6. }

3: dynamically allocate and manage the memory by yourself
In essence, to dynamically implement a two-dimensional array, you need to apply for more memory points, that is, to store the memory of one-dimensional array pointers.
The following method puts "memory for storing one-dimensional array Pointers" indexsize and memory H * rowsize for storing data together. This is really clever,
When the memory is released, delete the statement.

  1. Void ** malloc2d (int w, int H, int size)
  2. {
  3. Int J;
  4. Int rowsize = W * size;
  5. Int indexsize = H * sizeof (void *);
  6. Void ** A = (void **) malloc (indexsize + H * rowsize );
  7. Char * datastart = (char *) A + indexsize;
  8. For (j = 0; j
  9. A [J] = datastart + J * rowsize;
  10. Return;
  11. }

The W and H parameters are the columns and rows of the applied two-dimensional array, and the size is the number of bytes of the array unit. For example, to apply for a 4*5 Int-type two-dimensional array, use:
Int ** M = (INT **) malloc2d (5, 5, sizeof (INT ));
Directly use M [x] [Y] to reference the value of column Y in row X.
Use free (m) directly when returning.

Reference: http://blog.csdn.net/hanbf/archive/2007/08/31/1767645.aspx

 

//////////////////////////////////////// ////////////////////
The following is common in programming. After the two-dimensional array address is passed through the function pointer,
How to access array elements in a function is discussed here.
1) if the raw data is essentially one-dimensional, the occupied space only contains the size of the raw data and does not include the storage space for row pointers.
Then, eg int A [2] [2]; // memory allocated on the stack. sizeof (A) = 2*2 * sizeof (INT)
You can only access array elements in one dimension.

  1. Void test3 (INT ** A, int dim_ I, int dim_j)
  2. {// Int ** A is passed here, which is meaningless and must be converted to int *
  3. // Note that here, A is just a pure pointer, and the length of the dimension maintained by the compiler is lost. Therefore,
  4. // It can only be used as a one-dimensional array
  5. Int * P = (int *) A; // you can honestly access data by using a one-dimensional array.
  6. For (INT I = 0; I <dim_ I; ++ I)
  7. {
  8. For (Int J = 0; j <dim_j; ++ J)
  9. {
  10. * (P + I * dim_j + J) = I * J;
  11. }
  12. }
  13. Return;
  14. }

2: If the applied memory contains not only space occupied by data, but also space occupied by row pointers, you can access it through.

See test4 in the following function.

 

  1. Void Test2 (int A [] [2], int dim_ I, int dim_j)
  2. {
  3. A [1] [1] = 1;
  4. Return;
  5. }
  6. //: Applicable to two-dimensional arrays without row pointers.
  7. //: A two-dimensional array classified by EG 1 on the stack, or an int A [2] [2] without a row pointer
  8. //: Eg 2 int ** Pa = (INT **) malloc (sizeof (INT) * 2*2). Two rows and two columns
  9. Void test3 (INT ** A, int dim_ I, int dim_j)
  10. {
  11. // Note that here, A is just a pure pointer, and the length of the dimension maintained by the compiler is lost. Therefore,
  12. // It can only be used as a one-dimensional array
  13. Int * P = (int *) A; // you can honestly access data by using a one-dimensional array.
  14. For (INT I = 0; I <dim_ I; ++ I)
  15. {
  16. For (Int J = 0; j <dim_j; ++ J)
  17. {
  18. * (P + I * dim_j + J) = I * J;
  19. }
  20. }
  21. Return;
  22. }
  23. //: Applicable to two-dimensional arrays containing row pointer data
  24. //: EG malloc2d
  25. Void test4 (INT ** A, int dim_ I, int dim_j)
  26. {
  27. For (INT I = 0; I <dim_ I; ++ I)
  28. {
  29. For (Int J = 0; j <dim_j; ++ J)
  30. {
  31. A [I] [J] = (I + 1) * (J + 1 );
  32. }
  33. }
  34. Return;
  35. }
  36. Void ** malloc2d (int w, int H, int size)
  37. {
  38. Int J;
  39. Int rowsize = W * size;
  40. Int indexsize = H * sizeof (void *);
  41. Void ** A = (void **) malloc (indexsize + H * rowsize );
  42. Char * datastart = (char *) A + indexsize;
  43. For (j = 0; j
  44. A [J] = datastart + J * rowsize;
  45. Return;
  46. }
  47. Int main ()
  48. {
  49. // Method 1
  50. Int ** pa2 = (INT **) malloc2d (2, 2, sizeof (INT ));
  51. Test4 (pa2, 2, 2 );
  52. Cout <"pa2 [1] [1]" <pa2 [1] [1] <Endl;
  53. // Method 2
  54. Int A [2] [2] = {0, 0}, {0, 0 }};
  55. Test3 (INT **) A, 2, 2 );
  56. // Method 3
  57. Int M = 3;
  58. Int n = 3;
  59. Vector <vector <int> myvec (M, vector <int> (n ));
  60. For (INT I = 0; I <m; ++ I)
  61. {
  62. For (Int J = 0; j <n; ++ J)
  63. {
  64. Myvec [I] [J] = I * J;
  65. }
  66. }
  67. }

There are also many other methods, which are not listed here:
For other methods, refer to the following address:
Http://blog.csdn.net/smilelance/archive/2006/10/09/1327326.aspx

Transferred from shuimu Tsinghua
1.
A (* GA) [N] = new A [m] [N];
...
Delete [] GA;
Disadvantage: N must be known
Advantages: intuitive calling, continuous storage, and simple program (tested, the Destructor can be correctly called)

2. A ** GA = new A * [m];
For (INT I = 0; I <m; I ++)
Ga [I] = new A [n];
...
For (INT I = 0; I <m; I ++)
Delete [] Ga [I];
Delete [] GA;
Disadvantages: non-continuous storage, cumbersome procedures, GA is a ** type
Advantage: The call is intuitive. N can be unknown.

3. A * GA = new A [M * n];
...
Delete [] GA;
Disadvantage: The call is not intuitive enough.
Advantage: continuous storage, N can be unknown

4. Vector <vector <A> ga;
Ga. Resize (m); // these three rows can be used
For (INT I = 1; I <n; I ++ )//
Ga [I]. Resize (n );//
...

Disadvantages: non-continuous storage, inconvenient debugging, lower Compilation speed, and program expansion (the actual speed is not much different)
Advantage: intuitive call, automatic analysis and memory release, can call STL related functions, dynamic growth

5. Vector <A> ga;
Ga. Resize (M * n );
Combination of method 3 and 4

Release (provided by Penrose, thank you)
A ** GA = new A * [m];
Ga [0] = new A [M * n];
For (INT I = 1; I <m; I ++)
Ga [I] = Ga [I-1] + N;
...
Delete [] Ga [0];
Delete [] GA;
Disadvantage: The program is cumbersome. GA is of the ** type.
Advantage: continuous storage and intuitive calling. N can be unknown.

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.