Usage of "pointer" in C ++
Source: http://blog.chinaunix.net/space.php? Uid = 20498030 & Do = Blog & cuid = 274854
Many people are learning C ++, but they are very confused about the pointer in C ++. I will share it with you.
The system uses the memory of 4B to store a pointer. That is to say, if we apply for a pointer such as int * A = NULL, the system will allocate the memory of 4B for you, if we want to define another variable to store the pointer's memory address, we need a pointer, for example, int ** B = &;
"Pointer Pointer" is most commonly used in function value passing and array usage. I haven't met any other usage yet.
Let's talk about functions first.
There are three memory allocation methods:
1. Distribution from the static storage area;
2. stack allocation;
3. Allocate from the stack. New and malloc are all memory allocated from here.
Function execution is performed on the stack, and local variables in the function are allocated on the stack. These variables are destroyed when the function execution ends and returns, therefore, do not return the Data Pointer on the stack when the function returns. Otherwise, the returned Pointer Points to junk data.
Generally, the return value of a function is enough for us to use. However, if we need to return more than two parameters, or the return value of a function has other purposes (such as returning an error code ), the return value of our function is obtained from the parameter. In this case, we need to define a variable and pass the address of the variable to the function, modify the content of this address during function execution. Because this address is not in the stack where the function is located, it will not be destroyed when the function returns, so that the return value is achieved.
For example, a function is defined as follows:
BOOL func(int *a) { *a = 2; return TRUE; }
Function call:
int nAge; BOOL nAge = func(&nAge);
However, if the original variable is a pointer, such as int * page = NULL, you need to pass the pointer address to the function to modify the pointer value:
Int * page;
Bool Nage = func (& page );
The func function should be written as follows:
Bool func (INT **)
{
// A is the pointer, * a is the pointer, ** A is the pointer to the place, an int Value
* A = 2; // This is to assign a value to the pointer,
** A = 3; // This is the value to point to the pointer.
Return true;
} // Do not use this function. It assigns 3 to 4 bytes of the memory address 2 ......
Of course, a pointer is not required for an int variable, but if it is a linked list node, the linked list node is pointed by a pointer. You need to modify the pointer in the function, the pointer address will be passed in, so the pointer address will be stored with the pointer.
In a word, a pointer is a variable that stores the address of a pointer. It is a variable that occupies 4b of storage space and a real thing. It is used to transmit pointer values in functions.
If you still find any other use, you will be eager to post it. Thank you!
The pointer is enough. As for the pointer, it seems useless.
In addition, if you are interested in COM, you will find that many COM functions use the Double Star ** parameter, which all means this.
The above is only my personal understanding. If you have any mistakes, please join us! :)