It took a long time from the previous article, many people asked me why I didn't write, in fact, the reason is very simple, I can't write something, just as the previous note is what before this is just to Recommend a good book to your friends and help beginners are familiar with the author's thinking habits, but it is also very beautiful, such things are not suitable for too much (the things you know, or reading the book is good ). This means that the topic must be selected (of course, greatly increase the opportunity to make mistakes). The above words are an explanation for friends who have been supporting my friends, and I hope to get your support.
In this text, the topic I have chosen is the pointer, yes, I have heard a lot of people to complain about this trouble, but I have to say that I have to leave this thing (that troublesome damn) you are very difficult. What is going to do, this is a problem that C / C programmers is an impossible avoidance, then we'd better look at this thing in trouble, which is more solved than complaining.
First, the definition and initialization of the pointer: Detailed description in this regard in this respect, it is very simple, the pointer is an indirect operation object, and the pointer is stored in memory in memory. However, please note that if the pointer is just an address, things must be more, but the pointer must be consistent with the type of object he expressed, as an example of this book P73: int * p = null; " Double DVAL = 3.14; P = & DVAL; // Error is not p physical unable to store DVAL's address, but the two types of memory layout and content is completely different (for compilers), the compiler has only started from one Just reject this type of assignment, of course, from the moment you start watching the code above, I know that you don't think it is, a person who has learned programming will not make this kind of low-level mistake, really like this? So Ok, I want sometimes you will want to assign an address directly to the pointer. If you will write this? INT * P = 0x00001010; then, the compiler begins to swear, "0x00001010 is the address of that object? what type? Be int 通? Do you let me explain him? Overord! "Oh, it is no wonder that he is so temper, you have not told the compiler type information at all, of course, if you have to do this, you want to tell him the type of address: int * p = (int *) 0x00001010 Either you have anything about the type: "void * p = 0x00001010; but no matter what way you choose, CAST is always eloquent, it is recommended to use less.
Second, wild pointer: When the pointer assigns, the compiler will check the type of information of the address, which is very good, but he doesn't matter after assignment, this is the root of all troubles, such as a typical code: #include
#include
Using namespace std;
INT * f ()
{
INT A = 0;
Return & a;
}
INT g ()
{
Double A;
CIN >> A;
Return 0;
}
int main ()
{
INT * P = f ();
COUT << * P << endl;
g ();
COUT << * P << endl; system ("pause");
Return 0;
}
We found that the compiler did not report an error, and the address stored in the P was not changed, but the results of the two programs were completely different, because once the function f ended, local object A entered the stack shock, but please pay attention The stack pulverizer is not the address number and data of the object. But the type of object address of the object (of course this is the compiler behavior, and the pure memory is independent). The pointer is still in accordance with the previous agreement, and once the object address is changed, it is inevitably errors. The type information of a pointer pointing to the object is changed to be eliminated, so that the result of the pointer operation is unpredictable, it becomes a wild pointer.
Third, memory leakage: This problem has a direct relationship with the heap, first of all, we must understand what is memory leak, is the following function F memory leak? INT * f ()
{
INT * P = New (int);
Return P;
}
int main ()
{
INT * P = f ();
* p = 11;
COUT << * P << endl;
System ("pause");
Delete P;
Return 0;
}
We still refer to a paragraph in "Effective C " to answer this question: The reason for causing memory leaks is lost by memory allocation. If there is no mechanism outside of garbage disposal or other languages, these memory will not be recovered.
Obviously we did not throw down the pointer to the address, and the assigned memory is still in our hands. But it is quite important to pay attention. Especially when there is an abnormality, don't forget that DELETE this memory before the program exits. (Of course sometimes Auto_Ptr is also a good choice)