Use of C ++ 2D arrays

Source: Internet
Author: User

1. Type
A (* GA) [N] = new A [m] [N];
...
Delete Ga [];
Disadvantage: N must be a known advantage:
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. Advantages: intuitive calling,
N can be unknown

3.
A * GA = new A [M * n];
...
Delete [] GA;
Disadvantage: The call is not intuitive. Advantages:
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 a ** type. Advantages: continuous storage and intuitive calling. N can be unknown.

Appendix:, and the corresponding malloc-free versions I personally recommend 1 and can be replaced by 4, and calls are too cumbersome. After all, the source program is not used for running.

 

Below are some errors and unsuccessful versions

1. A * GA = new A [m] [N]; inevitable error

2. Vector <A [n]> ga; GA. Resize (m );
Compilation fails in GCC 3.2. I don't know how other compilers work or whether the standard permits.

 

Dynamic Two-dimensional array generation:

The traditional solution is to assign a pointer array and initialize each pointer As a dynamically allocated ''column ". The following is a two-dimensional example:

// Typedef int (* arraypoiter) [ncolumns];

Int ** dynamic_alloc_arrays (unsigned int nrows, unsigned int ncolumns)

{

Unsigned int I;

Int ** array = (INT **) malloc (nrows * sizeof (int *));

For (I = 0; I <nrows; I ++)

Array [I] = (int *) malloc (ncolumns * sizeof (INT ));

 

Printf ("array = 0x % x/N", (INT) array );

For (I = 0; I <nrows; I ++)

{

Printf ("array [% d] = 0x % x/N", I, (INT) array [I]);

}

Printf ("/N ");

Return array;

}
 

Void main (void)

{

Int ** test_allocate;

Unsigned int nrows = 3;

Unsigned int ncolumns = 4;

 

Test_allocate = dynamic_alloc_arrays (nrows, ncolumns );

Printf ("test_allocate = % x/N", test_allocate );

}

 

Of course, in real code, all malloc return values must be checked. You can also use sizeof (* array) and sizeof (** array) to replace sizeof (int *) and sizeof (INT) (because * array type is int *, ** the array type is int ).

You can make the Array Memory continuous, but it will be difficult to re-allocate columns later. You have to use a pointer arithmetic:

Int ** dynamic_alloc_arrays (unsigned int nrows, unsigned int ncolumns)

{

Unsigned int I;

Int ** array = (INT **) malloc (nrows * sizeof (int *));

Array [0] = (int *) malloc (nrows * ncolumns * sizeof (INT ));

For (I = 1; I <nrows; I ++)

Array [I] = array [0] + I * ncolumns;

Printf ("array = 0x % x/N", (INT) array );

For (I = 0; I <nrows; I ++)

{

Printf ("array [% d] = 0x % x/N", I, (INT) array [I]);

}

Printf ("/N ");

Return array;

}

Void main (void)

{

Int ** test_allocate;

Unsigned int nrows = 3;

Unsigned int ncolumns = 4;

Test_allocate = dynamic_alloc_arrays (nrows, ncolumns );

Printf ("test_allocate = % x/N", test_allocate );

}

 

In either case, dynamic Array members can be accessed using the normal array subscript arrayx [I] [J] (for 0 <= I <nrows and 0 <= j <ncolumns ).

Another option is to use an array pointer:

INT (* array4) [ncolumns] = malloc (nrows * sizeof (* array4 ));

However, this syntax becomes terrible and the runtime can only determine one dimension at most. Because ncolumns must be set

 

×××××××××××××××××××××××××××××××××××××××××

In C language, array names are used as pointers, one-dimensional arrays are pointers, two-dimensional arrays are pointers to pointers, and three-dimensional arrays are ......... is that true ?? See the following example:

Void show (INT ** info, int X, int y) // print the content of an array of x * y

{

Int I, J;

For (I = 0; I <X; I ++)

{

For (j = 0; j <Y; j ++)

{

Printf ("% d", info [I] [J]);

}

Printf ("/N ");

}

}

Void function (void)

{

Int as [10] [10];

Show (AS, 10, 10 );

// Error c2664: 'show': cannot convert parameter 1 from 'int [10] [10] 'to 'int ** 'types pointed to are unrelated; conversion requires reinterpret_cast, c-style cast or function-style cast

}

There is no security type check in C. The above program is only warning, but the program runs will crash

In C ++, it cannot be compiled at all, that is, as [10] [10] and INT ** are not a type at all.

 

Why? In C, although a two-dimensional array is defined as a pointer to a pointer, the actually pointed pointer does not exist, that is, there is no memory to store this pointer, only a pointer is returned when the as [N] is executed. As refers to the address that stores the array content !!

 

In fact, we can see from the above ** P and the use of dynamic two-dimensional arrays, ** essential differences between P and static two-dimensional arrays!

Another two-dimensional Dynamic Array Method

 

# Include <stdio. h>
# Include <malloc. h>
# Include <stdlib. h>

Int * ywshuzumalloc (int n) // One-dimensional array allocation
{
Int *;
A = (int *) malloc (sizeof (INT) * n );
Return;
}

Int ** ewshuzumalloc (int m, int N) // two-dimensional array allocation
{
Int **;
Int I;
A = (INT **) malloc (M * sizeof (int *));
For (I = 0; I <m; I ++)
A [I] = (int *) malloc (N * sizeof (INT ));
Return;
}

Int *** swshuzumalloc (int m, int N, int o) // 3D array allocation
{
Int ***;
Int I, J;
A = (INT ***) malloc (M * sizeof (INT **));
For (I = 0; I <m; I ++)
A [I] = (INT **) malloc (N * sizeof (int *));
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++)
A [I] [J] = (int *) malloc (O * sizeof (INT ));
}
Return;
}

Void main ()
{
Int *;
Int ** B;
Int *** C;
Int I, J, K, M, N, O;
Int lenght;
Printf ("Enter the number of dynamically allocated one-dimensional arrays:/N ");
Scanf ("% d", & N );
A = ywshuzumalloc (N );
For (I = 0; I <n; I ++)
{
A [I] = I + 1;
Printf ("% d", a [I]);
}
Printf ("/N ");

Lenght = N;
Printf ("the length of this array is: % d", lenght );
Printf ("/N ");

Printf ("Enter the number of dynamically allocated two-dimensional arrays:/N ");
Scanf ("% d", & M, & N );
B = ewshuzumalloc (m, n );
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++)
{
B [I] [J] = (I + 1) * 10 + J + 1;
Printf ("% d", B [I] [J]);
}
Printf ("/N ");
}
Printf ("/N ");
Lenght = m * N;
Printf ("the length of this array is: % d", lenght );
Printf ("/N ");

Printf ("Enter the number of dynamically allocated 3D Arrays:/N ");
Scanf ("% d", & M, & N, & O );
C = swshuzumalloc (M, N, O );
For (I = 0; I <m; I ++)
{
For (j = 0; j <n; j ++)
{
For (k = 0; k <O; k ++)
{
C [I] [J] [k] = (I + 1) * 100 + (J + 1) * 10 + k + 1;
Printf ("% d", C [I] [J] [k]);
}
Printf ("/N ");
}
Printf ("/N ");
}
Printf ("/N ");
Lenght = m * n * O;
Printf ("the length of this array is: % d", lenght );
Printf ("/N ");

}

 

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.