8.6. Using const with Pointers

Recall that const enables you to inform the compiler that the value of a particular variable should not be modified. Many possibilities exist for using (or not using) const with function parameters, so how do you choose the most appropriate? Let the principle of least privilege be your guide. Always give a function enough access to the data in its parameters to accomplish its specified task, but no more. This section discusses how to combine const with pointer declarations to enforce the principle of least privilege.

Chapter 6 explained that when an argument is passed by value, a copy of the argument is passed to the function. If the copy is modified in the called function, the original value in the caller does not change. In some instances, even the copy of the argument’s value should not be altered in the called function.

Consider a function that takes a pointer to the initial element of a built-in array and the array’s size as arguments and subsequently displays the built-in array’s elements. Such a function should loop through the elements and output each individually. The built-in array’s size is used in the function’s body to determine the highest subscript so the loop can terminate when the displaying completes. The size does not need to change in the function body, so it should be declared const to ensure that it will not change. Because the built-in array is only being displayed, it, too, should be declared const. This is especially important because built-in arrays are always passed by reference and could easily be changed in the called function. An attempt to modify a const value is a compilation error.


Image Software Engineering Observation 8.2

If a value does not (or should not) change in the body of a function to which it’s passed, the parameter should be declared const.



Image Error-Prevention Tip 8.4

Before using a function, check its function prototype to determine the parameters that it can and cannot modify.


There are four ways to pass a pointer to a function: a nonconstant pointer to nonconstant data, a nonconstant pointer to constant data (Fig. 8.10), a constant pointer to nonconstant data (Fig. 8.11) and a constant pointer to constant data (Fig. 8.12). Each combination provides a different level of access privilege.


 1   // Fig. 8.10: fig08_10.cpp
 2   // Attempting to modify data through a
 3   // nonconstant pointer to constant data.
 4
 5   void f( const int * ); // prototype
 6
 7   int main()
 8   {
 9      int y = 0;
10
11      f( &y ); // f will attempt an illegal modification
12   } // end main
13
14   // constant variable cannot be modified through xPtr
15   void f( const int *xPtr )
16   {
17      *xPtr = 100; // error: cannot modify a const object
18   } // end function f

GNU C++ compiler error message:


fig08_10.cpp: In function 'void f(const int*)':
fig08_10.cpp:17:12: error: assignment of read-only location '* xPtr'


Fig. 8.10. Attempting to modify data through a nonconstant pointer to const data.


 1   // Fig. 8.11: fig08_11.cpp
 2   // Attempting to modify a constant pointer to nonconstant data.
 3
 4   int main()
 5   {
 6      int x, y;
 7
 8      // ptr is a constant pointer to an integer that can       
 9      // be modified through ptr, but ptr always points to the  
10      // same memory location.                                  
11      int * const ptr = &x; // const pointer must be initialized
12
13      *ptr = 7; // allowed: *ptr is not const
14      ptr = &y; // error: ptr is const; cannot assign to it a new address
15   } // end main

Microsoft Visual C++ compiler error message:


you cannot assign to a variable that is const


Fig. 8.11. Attempting to modify a constant pointer to nonconstant data.


 1   // Fig. 8.12: fig08_12.cpp
 2   // Attempting to modify a constant pointer to constant data.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      int x = 5, y;
 9
10      // ptr is a constant pointer to a constant integer.
11      // ptr always points to the same location; the integer
12      // at that location cannot be modified.
13      const int *const ptr = &x;
14
15      cout << *ptr << endl;
16
17      *ptr = 7; // error: *ptr is const; cannot assign new value
18      ptr = &y; // error: ptr is const; cannot assign new address
19   } // end main

Xcode LLVM compiler error message:


Read-only variable is not assignable
Read-only variable is not assignable


Fig. 8.12. Attempting to modify a constant pointer to constant data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.65.65