Author: Andrew Peace Original link: http: //www.codeproject.com/cpp/pointers.asp Click to download the complete source code
What is a pointer?
The pointer is basically the same as other variables, the only point difference is that the pointer does not contain actual data, but includes an address pointing to the memory location, you can find some information in this address. This is a very important concept, and many programs or ideas are the design foundation for their design, such as a list.
begin
How to define a pointer? Hey, just like defining other variables, but you still need to add an asterisk before the variable name. For example, the following code creates two pointers pointing integers: int * pNumBerone; INT * PNUMBERTWO; Do not pay attention to the prefix "P" of the variable name? This is a habit of writing code to indicate that this variable is a pointer. Now let's point these pointers to some actual values: pnumberone = & some_number; pnumbertwo = & some_other_number; "&" flag should read "The address of (...", its role is to return a variable memory Address, not this variable itself. So in this example, PNumberone is the address of Some_Number, also known as PNumberone points to Some_Number. Now, if we want to use some_number's address, then we can use PNumberone. If we want to use the value of Some_Number via PNumberone, we can use * pNumberone. "*" You should read "The Memory Location Pointed to by" "pointing to the memory location)", it is used to obtain the value points to the pointer. However, the exception of the pointer declaration, such as "int * pnumber".
What I learned now (an example):
call out! I have to understand what I have, so I suggest that if you still don't understand the above concept, then it is best to read again; the pointer is a complex topic, to master it is going to spend some time. Here is an example that explains the concepts discussed above. It is written by C and does not extension of C . #include
trap!
See if you can point out the shortcomings of the following procedures: #include
Dynamic allocation may be a key technique for pointers. It is used to allocate memory without defining a variable, then pointing by a pointer to this memory. Although this concept seems to be confused, it is very simple. The following code explained how to allocate memory space for an integer: int * pNumber; pnumber = new int; first line of code declares a pointer PNUMBER, the second line of code assigns an integer space, and makes PNumber points to this section Allocated memory. Below is another example, this time uses a Double: double * PDOUBLE; PDOUBLE = New Double; these rules are the same T, so you should be able to master it easily. Dynamic allocation and local variables are: The memory you allocate does not be released when the function returns and the statement block ends, so if you use dynamic allocation to rewrite the above code, then it will work properly: #include
I have to understand, I understand
There is also a complicated factor and is very serious - although it is very remedied. The problem is that it is still intact when you assign, although it is still intact, this memory will never be destroyed automatically. That is to say, if you don't inform the computer ending, this memory will always exist, the result of this is the waste of memory. In the end, the system will crash because of memory consumption. So this is a quite important issue. When you use the memory, release its code very simple: delete ppoint; it is so simple. Anyway, in your passing a valid pointer - is also a pointing, you have already assigned a good memory pointer, not the old garbage memory - time, you need to be very careful. Try DELETE Solid memory is very dangerous, which may cause your program to crash. Ok, the following is that example, this time it doesn't waste memory: #include
It is very useful to pass the technique of the function to the function, but it is easy to master (translation: Is there a inevitable turning relationship? Hey, I can't see it, but since the author wrote, I can't find a suitable correlation word, I have Translate by literally). If we want to write a program, you have to increase a number of 5, we may write: #include
With regard to pointers, I still have two points to remind you. One of them is a pointer to the structure or class. You can define a class: class myclass {public: int m_number; char m_character;}; then you can define a Myclass variable: myclass think; you should already know these, if you haven't, you need to read it Information in this regard. You can define a pointer to myclass: Myclass * Thing; just like you expect. Then you can assign some memory for this pointer: Thing = new myclass; this is the problem. How will you use this pointer? Hey, usually you will write: "Thing.m_Number", but for this example, because Thing is not a MyClass, but a pointer to myclass, so it does not contain a variable named "m_number"; it The pointer to the structure contains this m_number. Therefore, we must use a different conversion method. This is to replace "." (Point) to a "->" (horizontal line and a larger number). Please see the following example: Class myclass {public: int m_number; char m_character;}; void main () {myclass * ppoint; ppointer = new myclass; ppointer-> m_number = 10; ppointer-> m_character = 's'; delete Point of PPOINTER;} Pointer to array
You can also make the pointer to the array, as follows: int * parray; parray = new int [6]; this will create a pointer PARRY, which will point to an array of 6 elements. Another way to use dynamic allocation is as follows: int * parray; int myArray [6]; parray = & myarray [0]; Please note that you can write only MyArray instead & myarray [0]. Of course, this approach is only applicable to arrays, which is the implementation of the C / C language (the translation: You can also assign the function name to a corresponding function pointer). The usual error is written as "parray = & myarray;", this is incorrect. If you write this, you will get a pointer to the array pointer (maybe some mouths?), This is of course not what you want.
Use pointer to array
If you have a pointer to an array, how will you use it? Hey, if you say, you have a pointer to an array of integers. This pointer will initially point to the first value of the array, look at the following example: #include
Finally, you should pay attention to: You can't delete the memory that is not allocated with new, like this: void main () {int number; int * pnumber = number; delete pnumber; // error: * PNumber is not using new assignment of}
Frequently Asked Questions and FAQ
Q: Why get "SMBOL undefined" error when using New and Delete? A: This is probably because your source file is explained by the compiler as a C file because the New and Delete operators are new features of C . The usual correction method is to use .cpp as your source file extension. Q: What is the difference between NEW and Malloc? A: New is a C unique keyword, and is a standard allocated memory method (except for the memory allocation method of the Windows program). You must never use Malloc in a C C program unless absolutely necessary. Since Malloc is not an object-oriented characteristic design, use it to assign memory for class objects, which will not call the class constructor, which will have problems. For these reasons, this article does not discuss them, and as long as it is possible, I will also avoid using them.
Q: Can I use free and delete? A: You should use and allocate the memory matching method to release memory. For example, using free to release memory allocated by malloc, use Delete to release memory allocated by the NEW.
Quote
From a certain point of view, the reference has exceeded the scope of this article. However, since many readers have asked me in this, I will conduct a brief discussion here. Quote and pointers are very similar, which can be used in many cases. If you can recall the contents of the above - I mentioned "&" Read as "The Address of (... The address)", in the declaration of the exception. In this case, it should read "A Reference to (... reference)", as follows: int & number = myothernumber; Number = 25; reference is like a pointer to my myothernumber, but it is an automatic analysis The address, so its behavior is like the actual value pointed to by the pointer. The pointer code with its equivalent is as follows: int * pNumber = & myothernumber; * pNumber = 25; another difference between the pointer and reference is that you cannot replace the content, that is, you can't replace the reference points after the declaration. For example, the following code will output 20: int myfirstnumber = 25; int myReference = MyfirstNumber; myreference = mysecondNumber; Printf ("% d", myfristnumber); When in the class, the reference value must be Constructor settings, like this: CMYCLASS :: CMYCLASS (int & variable): m_myreferenceincmyclass (variable) {// Here is constructor}
to sum up
This subject is originally difficult to master, so you'd better read it at least twice - because most people can't understand immediately. Let me list this paper below:
1. The pointer is a variable that points to a location in memory, you can define a pointer (iNT * Number) by adding an asterisk (*) before the variable name (*). 2, you can get its memory address by adding "&" in the variable name (which is pnumr = & my_number). 3. In addition to the statement (for example int * number), the asterisk should read "The Memory Location Pointed to by" The Memory Position Pointing by ... ". 4. In addition to in the statement (such as int & number), "&" should read "The Address of (...". " 5, you can use the "New" key to allocate memory. 6, the pointer must match the variable type it points to, so INT * NUMBER should not point to a MyClass. 7, you can pass pointers to the function. 8, you must use the "delete" keyword to release the memory you assigned. 9. You can use & Array [0] to get a array of pointers. 10, you must use delete [] to release the array of dynamic assignments, not simple delete. This is not a complete pointer guide, one thing I can involve other details, such as pointers of pointers; there are some things that I have not involved, such as function pointers - I think as beginners, this Some are more complicated; there are some things that are rarely used, I have not mentioned here, provision of these un practical details make everyone confusing.
That's all! You can try to run the program in this article and write some examples to understand the problem about the pointer.