Comparison of dynamic distribution methods for C/C ++ 2D arrays

Source: Internet
Author: User

Method 1:
1 # include <iostream>
2 using namespace std;
3
4 // by snape 2012-3-25
5 // method 1: drawback is we need calculate the one-dimen1_index to access the 2D array
6 int main ()
7 {
8 int rowSize, colSize, totalSize;
9 int index, I, j;
10
11 cout <"Enter the row and column size for your 2D array! "<Endl;
12 cin> rowSize> colSize;
13
14 totalSize = rowSize * colSize;
15 int * pArray;
16 pArray = new int [totalSize];
17
18 // file the array with integers from 0 to totalsize
19 // file into ss the rows, moving down the colums
20
21 int arrayValue = 0;
22 for (I = 0; I <rowSize; ++ I) // outer-loop traverse down the "rows"
23 {
24 for (j = 0; j <colSize; ++ j)
25 {
26 // caculate array index
27 // index = rowSize * j + I; // both index = rowSize * j + I; and index = colSize * I + j; are OK
28 index = colSize * I + j; // but if index = rowsize * I + j; or index = colSize * j + I; then there will be a bug.
29 pArray [index] = arrayValue; // I like index = colSize * I + j; since the arrange of 2D is according to rows
30 cout <"index =" <index <endl;
31 cout <"I (row) =" <I <"j (col) = "<j <" array value "<pArray [index] <endl;
32 ++ arrayValue;
33}
34}
35
36 // output the array
37 for (int k = 0; k <totalSize; ++ k)
38 {
39 cout <pArray [k] <endl;
40}
41 cout <"The End" <endl;
42 delete [] pArray;
43 return 0;
44}
45
46

Method 2:
1 // by snape
2 // method 2: better than method 1, but call new twice
3 int main ()
4 {
5 int rowSize, colSize, totalSize;
6 int I, j;
7 cout <"Enter the row and column size for your 2D array" <endl;
8 cin> rowSize> colSize;
9
10 totalSize = rowSize * colSize;
11
12 int * pArray; // pointer to an integer
13 int ** pPointerArray; // pointer to an integer pointer
14
15 pArray = new int [totalSize]; // memory for totalSize integers
16 pPointerArray = new int * [rowSize]; // memory for rowSize # of int pointers
17
18 // fill the pointer array with the pArray [I] [0] address
19 for (I = 0; I <rowSize; ++ I)
20 pPointerArray [I] = pArray + I * colSize; // place the address into the pointer
21
22 // now fill the pArray by using the pPointerArray to access elements
23 int arrayValue = 0;
24 for (I = 0; I <rowSize; ++ I)
25 {
26 for (j = 0; j <colSize; ++ j)
27 {
28 pPointerArray [I] [j] = arrayValue; // cool
29 cout <"I (row) =" <I <"j (col) = "<j <" array value = "<pPointerArray [I] [j] <endl;
30 + arrayValue;
31}
32}
33
34 // output the array
35 for (int k = 0; k <totalSize; ++ k)
36 {
37 cout <pArray [k] <endl;
38}
39 cout <"The End! "<Endl;
40 delete [] pArray;
41 delete [] pPointerArray;
42 return 0;
43}

Method 3:
1 // by snape
2 // method 3: better than method 2. just malloc once and the memory is contiguous block. the best
3 int ** my2DAlloc (int rowSize, int colSize)
4 {
5 int I;
6 int header = rowSize * sizeof (int *);
7 int data = rowSize * colSize * sizeof (int );
8 int ** rowptr = (int **) malloc (header + data); // malloc memory for both data and pointerArray (the header)
9
10 if (rowptr = NULL)
11 return NULL;
12 int * buf = (int *) (rowptr + rowSize); // buf: the pointer to the first data
13 for (I = 0; I <rowSize; ++ I) // assign the address of each row to pointerArray (the header)
14 rowptr [I] = buf + I * colSize;
15
16 return rowptr;
17}
18
19 int main ()
20 {
21 cout <"Enter the row and column size for your 2D array" <endl;
22 int rowSize, colSize;
23 cin> rowSize> colSize;
24 int ** p = my2DAlloc (rowSize, colSize );
25
26 // assign values
27 int I, j, arrayValue = 0;
28 for (I = 0; I <rowSize; ++ I)
29 for (j = 0; j <colSize; ++ j)
30 p [I] [j] = arrayValue ++;
31
32 // output values
33 for (I = 0; I <rowSize; ++ I)
34 for (j = 0; j <colSize; ++ j)
35 cout <p [I] [j] <endl;
36
37 free (void *) p );
38}

Method 3: I think it is best to call malloc only once. The space is continuous and it is convenient to release the memory.

Do you have any ideas?

 

From chenglong7997

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.