Let's take a look at a line of code:
Const int * p = new int (10 );
The problem arises. The new address should be block heap memory, and the const constant should be stored in the static memory zone, is the memory pointed to by the pointer p in the static zone or heap?
Let's take a look at the usage of const?
- Const constants can be defined.
- Const can modify the parameters, return values, and even the definition body of a function. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.
In fact, it is very easy to think about it. const int * p is a constant pointer, that is, through which the target cannot be modified, but without saying that the target pointed by the pointer must be a constant.
Int n = 100; // n is not a constant const int * p1 = & n; // p1 points to nint * p2 = & n; // p2 points to nn = 200; // OK, n is not a constant, you can modify * p1 = 300; // error, p1 is a constant pointer, and its target cannot be modified * p2 = 400; // OK, p2 is not a constant pointer and its target can be modified.
Therefore, const int * p defines a constant, which indicates that the content pointed to by p cannot be modified. The int memory to be pointed to should be in the heap. The pointer type determines the behavior of the pointer. As for the target to which the Pointer Points, what should be done? It has nothing to do with this pointer.
For more information, see C pointer: const int * pi semantics.