B. Stroustrup - The C++ Programming Language (794319), страница 46
Текст из файла (страница 46)
Most advantages of the built-in array and few of the disadvantages can be obtained through the use of the standard-library container array (§8.2.4, §34.2.1).Some C++ implementations offer optional range checking for arrays. However, such checking canbe quite expensive, so it is often used only as a development aid (rather than being included in production code). If you are not using range checking for individual accesses, try to maintain a consistent policy of accessing elements only in well-defined ranges. That is best done when arrays aremanipulated through the interface of a higher-level container type, such as vector, where it is harderto get confused about the range of valid elements.7.4.2 Multidimensional ArraysMultidimensional arrays are represented as arrays of arrays; a 3-by-5 array is declared like this:int ma[3][5];// 3 arrays with 5 ints eachWe can initialize ma like this:void init_ma(){for (int i = 0; i!=3; i++)for (int j = 0; j!=5; j++)ma[i][j] = 10∗i+j;}or graphically:ma:00 01 02 03 04 10 11 12 13 14 20 21 22 23 24The array ma is simply 15 ints that we access as if it were 3 arrays of 5 ints.
In particular, there isno single object in memory that is the matrix ma – only the elements are stored. The dimensions 3and 5 exist in the compiler source only. When we write code, it is our job to remember them somehow and supply the dimensions where needed. For example, we might print ma like this:void print_ma(){for (int i = 0; i!=3; i++) {for (int j = 0; j!=5; j++)cout << ma[i][j] << '\t';cout << '\n';}}The comma notation used for array bounds in some languages cannot be used in C++ because thecomma (,) is a sequencing operator (§10.3.2).
Fortunately, most mistakes are caught by the compiler. For example:int bad[3,5];int good[3][5];int ouch = good[1,4];int nice = good[1][4];// error: comma not allowed in constant expression// 3 arrays with 5 ints each// error: int initialized by int* (good[1,4] means good[4], which is an int*)184Pointers, Arrays, and ReferencesChapter 77.4.3 Passing ArraysArrays cannot directly be passed by value. Instead, an array is passed as a pointer to its first element.
For example:void comp(double arg[10]){for (int i=0; i!=10; ++i)arg[i]+=99;}// arg is a double*void f(){double a1[10];double a2[5];double a3[100];comp(a1);comp(a2);comp(a3);// disaster!// uses only the first 10 elements};This code looks sane, but it is not. The code compiles, but the call comp(a2) will write beyond thebounds of a2. Also, anyone who guessed that the array was passed by value will be disappointed:the writes to arg[i] are writes directly to the elements of comp()’s argument, rather than to a copy.The function could equivalently have been written asvoid comp(double∗ arg){for (int i=0; i!=10; ++i)arg[i]+=99;}Now the insanity is (hopefully) obvious. When used as a function argument, the first dimension ofan array is simply treated as a pointer.
Any array bound specified is simply ignored. This impliesthat if you want to pass a sequence of elements without losing size information, you should notpass a built-in array. Instead, you can place the array inside a class as a member (as is done forstd::array) or define a class that acts as a handle (as is done for std::string and std::vector).If you insist on using arrays directly, you will have to deal with bugs and confusion without getting noticeable advantages in return. Consider defining a function to manipulate a two-dimensionalmatrix.
If the dimensions are known at compile time, there is no problem:void print_m35(int m[3][5]){for (int i = 0; i!=3; i++) {for (int j = 0; j!=5; j++)cout << m[i][j] << '\t';cout << '\n';}}Section 7.4.3Passing Arrays185A matrix represented as a multidimensional array is passed as a pointer (rather than copied; §7.4).The first dimension of an array is irrelevant to finding the location of an element; it simply stateshow many elements (here, 3) of the appropriate type (here, int[5]) are present. For example, look atthe layout of ma above and note that by knowing only that the second dimension is 5, we can locatema[i][5] for any i.
The first dimension can therefore be passed as an argument:void print_mi5(int m[][5], int dim1){for (int i = 0; i!=dim1; i++) {for (int j = 0; j!=5; j++)cout << m[i][j] << '\t';cout << '\n';}}When both dimensions need to be passed, the ‘‘obvious solution’’ does not work:void print_mij(int m[][], int dim1, int dim2)// doesn’t behave as most people would think{for (int i = 0; i!=dim1; i++) {for (int j = 0; j!=dim2; j++)cout << m[i][j] << '\t';// sur prise!cout << '\n';}}Fortunately, the argument declaration m[][] is illegal because the second dimension of a multidimensional array must be known in order to find the location of an element.
However, the expressionm[i][j] is (correctly) interpreted as ∗(∗(m+i)+j), although that is unlikely to be what the programmerintended. A correct solution is:void print_mij(int∗ m, int dim1, int dim2){for (int i = 0; i!=dim1; i++) {for (int j = 0; j!=dim2; j++)cout << m[i∗dim2+j] << '\t'; // obscurecout << '\n';}}The expression used for accessing the members in print_mij() is equivalent to the one the compilergenerates when it knows the last dimension.To call this function, we pass a matrix as an ordinary pointer:int test(){int v[3][5] = {{0,1,2,3,4}, {10,11,12,13,14}, {20,21,22,23,24}};186Pointers, Arrays, and ReferencesChapter 7print_m35(v);print_mi5(v,3);print_mij(&v[0][0],3,5);}Note the use of &v[0][0] for the last call; v[0] would do because it is equivalent, but v would be atype error. This kind of subtle and messy code is best hidden.
If you must deal directly with multidimensional arrays, consider encapsulating the code relying on it. In that way, you might ease thetask of the next programmer to touch the code. Providing a multidimensional array type with aproper subscripting operator saves most users from having to worry about the layout of the data inthe array (§29.2.2, §40.5.2).The standard vector (§31.4) doesn’t suffer from these problems.7.5 Pointers and constC++ offers two related meanings of ‘‘constant’’:• constexpr: Evaluate at compile time (§2.2.3, §10.4).• const: Do not modify in this scope (§2.2.3).Basically, constexpr’s role is to enable and ensure compile-time evaluation, whereas const’s primary role is to specify immutability in interfaces.
This section is primarily concerned with the second role: interface specification.Many objects don’t have their values changed after initialization:• Symbolic constants lead to more maintainable code than using literals directly in code.• Many pointers are often read through but never written through.• Most function parameters are read but not written to.To express this notion of immutability after initialization, we can add const to the definition of anobject. For example:const int model = 90;const int v[] = { 1, 2, 3, 4 };const int x;// model is a const// v[i] is a const// error : no initializerBecause an object declared const cannot be assigned to, it must be initialized.Declaring something const ensures that its value will not change within its scope:void f(){model = 200;v[2] = 3;}// error// errorNote that const modifies a type; it restricts the ways in which an object can be used, rather thanspecifying how the constant is to be allocated. For example:void g(const X∗ p){// can’t modify *p here}Section 7.5void h(){X val;g(&val);// ...}Pointers and const187// val can be modified hereWhen using a pointer, two objects are involved: the pointer itself and the object pointed to.
‘‘Prefixing’’ a declaration of a pointer with const makes the object, but not the pointer, a constant. Todeclare a pointer itself, rather than the object pointed to, to be a constant, we use the declaratoroperator ∗const instead of plain ∗. For example:void f1(char∗ p){char s[] = "Gorm";const char∗ pc = s;pc[3] = 'g';pc = p;// pointer to constant// error : pc points to constant// OKchar ∗const cp = s;cp[3] = 'a';cp = p;// constant pointer// OK// error : cp is constantconst char ∗const cpc = s;cpc[3] = 'a';cpc = p;// const pointer to const// error : cpc points to constant// error : cpc is constant}The declarator operator that makes a pointer constant is ∗const.