I thought I had mastered the pointer, but I was not quite sure about the problem. Ask!
Program 1:
void Mymalloc (char *s) // I want to allocate memory in the function and return { s= (char *) malloc (a); } void Main () { char *p=NULL; Mymalloc (p); // here P is still the value of null,p not change, why? if (p) Free (P); }
Program 2:
void Mymalloc (char* *s) { *s= (char *) malloc(100 ); } void Main () { char *p=NULL; Mymalloc (&p); // the p here can get the correct value. if (p) Free (P); }
Program 3:
#includevoidFunint*p) {intb= -; P=&b; } main () {intA=Ten; int*Q; Q=&A; printf ("%d\n",*q); Fun (q); printf ("%d\n",*q); return 0; }
Result is
10
10
Program 4:
#includevoidFunint*p) {*p= -; } Main () {intA=Ten; int*Q; Q=&A; printf ("%d\n",*q); Fun (q); printf ("%d\n",*q); return 0; }
Result is
10
100
Why?
---------------------------------------------------------------
1. The allocated memory is the row parameter S,p no memory allocated
2. Allocated memory is the pointer p pointing to the line parameter S, so the memory is allocated
---------------------------------------------------------------
is not the pointer does not understand, is the function calls the question! Look at this paragraph:
7.4 How does the pointer parameter pass memory?
If the parameter of the function is a pointer, do not expect to use the pointer to apply dynamic memory. In the example 7-4-1, the statement getmemory (STR, 200) of the test function does not have the desired memory for STR, and STR is still null, why?
voidGetMemory (Char*p,intnum) {P= (Char*)malloc(sizeof(Char) *num); } voidTest (void) { Char*str =NULL; GetMemory (str, -);//str is still NULLstrcpy (str,"Hello");//Run Error}
Example 7-4-1 attempting to request dynamic memory with pointer parameters
The problem is in the function getmemory. The compiler always makes a temporary copy of each parameter of the function, the copy of the pointer parameter P is _p, and the compiler causes _p = p. If the program inside the function modifies the contents of the _p, it causes the contents of the parameter p to be modified accordingly. This is why the pointer can be used as an output parameter. In this case, _p applied for new memory, but changed the memory address referred to by _p, but P did not change. So the function getmemory doesn't output anything. In fact, each time a getmemory is executed, a piece of memory is leaked because the memory is not freed with free.
If you have to use pointer parameters to request memory, you should instead use pointers to pointers, see example 7-4-2.
voidGetMemory2 (Char**p,intnum) { *p = (Char*)malloc(sizeof(Char) *num); } voidTest2 (void) { Char*str =NULL; GetMemory2 (&STR, -);//Note that the parameter is &STR, not strstrcpy (str,"Hello"); } voidFunint*p) {intb= -; P=&b; } main () {intA=Ten; int*Q; Q=&A; printf ("%d\n",*q); Fun (q);////Reason with the first procedure. printf"%d\n",*q); return 0; }
Result is
10
10
Program 4:
#includevoidFunint*p) {*p= -;//The memory units referred to by the parameter P and the argument p are the same. Therefore, changing the memory unit contents of the parameter P changes the actual parameter//the contents of the memory unit} main () {intA=Ten; int*Q; Q=&A; printf ("%d\n",*P); Fun (q); printf ("%d\n",*q); return 0; }
Result is
10
100
Why?
---------------------------------------------------------------
void main () { char *p=null; Mymalloc (P); // the p actually or null,p here doesn't change, why? if (p) free (p); void mymalloc (char *s) // I want to allocate memory in the function and return {s = (char *) malloc (100 ); }
mymalloc (P) execution: Note the pointer variable is just a special variable, in fact it is an integer value, but it is an address in memory. This address can be accessed through it.
Program 2:
void mymalloc (char **s) { *s= (char *) malloc (100 ); void main () { char *p=null; Mymalloc ( &p); // if (p) free (P); }
Program 2 is correct, why? Look at an execution process and you'll know:
Mymalloc (&p); The address of P is passed into the function, assuming that the address of the store p variable is 0x5555, then 0x5555 this address is the value of the pointer variable p, that is, Ox5555 points to p.
The call is also assigned a temporary variable char **s, when the value of S is &p value is 0x5555, but the space occupied by S is another space, except that the value it points to is an address: Ox5555.
*s= (char *) malloc (100); The meaning of this sentence is to assign the value of s to (char *) malloc (100) to the value pointed to in the position of 0x5555, while the 0x5555 position is exactly the pointer variable p, So the value of P becomes the value of (char *) malloc (100). That is, the value of P is the starting address of the newly allocated memory.
the point of this problem is that it is important to understand that a variable is assigned a copy when it is called as a function parameter, whether it is a value or a pass-through. when passed in, it is not related to the parameter, it does not change the value of the formal parameter. Mymalloc (p) does not change the value of P, and the value of P is of course NULL, which can only change the value of the memory address that P points to. But Mymalloc (&p) Why, it does not change the value (&P) is not possible to change, but it can change (&p) point to the memory address of the value, that is, the value of P.
---------------------------------------------------------------
What you need to figure out is the pointer variable and the variable that the pointer refers to (possibly a piece of memory).
Pointer variables are stored just like normal variables.
C + + pointer detailed analysis (GO)