Source: http://www.newsmth.net/pc/pccon.php? Id = 10002501 & nid = 337392
I thought I had mastered the pointer, but I still didn't quite understand this problem. Ask!
Procedure 1:
Void mymalloc (char * s) // I want to allocate memory in the function and then return
{
S = (char *) malloc (100 );
}
Void main ()
{
Char * P = NULL;
Mymalloc (p); // here, P is actually null, and the value of P is not changed. 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 );
}
Procedure 3:
# Include <stdio. h>
Void fun (int * P)
{
Int B = 100;
P = & B;
}
Main ()
{
Int A = 10;
Int * q;
Q = &;
Printf ("% d \ n", * q );
Fun (Q );
Printf ("% d \ n", * q );
Return 0;
}
Result:
10
10
Procedure 4:
# Include <stdio. h>
Void fun (int * P)
{
* P = 100;
}
Main ()
{
Int A = 10;
Int * q;
Q = &;
Printf ("% d \ n", * q );
Fun (Q );
Printf ("% d \ n", * q );
Return 0;
}
Result:
10
100
Why?
---------------------------------------------------------------
1. The allocated memory is the row parameter S, and P does not allocate memory.
2. The allocated memory is the pointer P directed by the row parameter S, so the memory is allocated.
---------------------------------------------------------------
The pointer is not clear, but it is a function call problem! Take a look at this section:
7.4 how does the pointer Parameter Pass the memory?
If the function parameter is a pointer, do not expect this pointer to apply for dynamic memory. In Example 7-4-1, the getmemory (STR, 200) Statement of the test function does not enable STR to obtain the expected memory. STR is still null. Why?
Void getmemory (char * P, int num)
{
P = (char *) malloc (sizeof (char) * num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (STR, 100); // STR is still null
Strcpy (STR, "hello"); // running error
}
Example 7-4-1 try to apply for dynamic memory with pointer Parameters
The fault lies in the getmemory function. The compiler always needs to make a temporary copy for each parameter of the function. The copy of the pointer parameter P is _ p, and the compiler makes _ p = P. If the program in the function body modifies the content of _ p, the content of parameter P is modified accordingly. This is why pointers can be used as output parameters. In this example, _ P applied for a new memory, but changed the memory address indicated by _ p, but P was not changed at all. Therefore, the getmemory function cannot output anything. In fact, each execution of getmemory will leak a piece of memory, because the memory is not released with free.
If you need to use the pointer parameter to request memory, you should use "pointer to Pointer" instead. See example 7-4-2.
Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (sizeof (char) * num );
}
Void Test2 (void)
{
Char * STR = NULL;
Getmemory2 (& STR, 100); // note that the parameter is & STR, not Str
Strcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}
Example 7-4-2 apply for dynamic memory with a pointer pointing to the pointer
Because the concept of "pointer to Pointer" is not easy to understand, we can use function return values to transmit dynamic memory. This method is simpler. See example 7-4-3.
Char * getmemory3 (INT num)
{
Char * P = (char *) malloc (sizeof (char) * num );
Return P;
}
Void test3 (void)
{
Char * STR = NULL;
STR = getmemory3 (100 );
Strcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}
Example 7-4-3 use the function return value to pass the dynamic memory
Although it is easy to use the function return value to pass dynamic memory, some people often use the return statement wrong. It is emphasized that the return statement should not be used to return the pointer pointing to the "stack memory", because the function exists in it automatically disappears when it ends. See example 7-4-4.
Char * getstring (void)
{
Char P [] = "Hello World ";
Return P; // the compiler will give a warning
}
Void test4 (void)
{
Char * STR = NULL;
STR = getstring (); // STR content is junk
Cout <STR <Endl;
}
Example 7-4-4 Return Statement returns a pointer to "stack memory"
Use the debugger to track test4 step by step. After executing the STR = getstring statement, STR is no longer a null pointer, but the STR content is not "Hello World" but garbage.
What if I rewrite Example 7-4-4 to Example 7-4-5?
Char * getstring2 (void)
{
Char * P = "Hello World ";
Return P;
}
Void test5 (void)
{
Char * STR = NULL;
STR = getstring2 ();
Cout <STR <Endl;
}
Example 7-4-5 return statement returns a constant string
Although the function test5 runs without errors, the design concept of the function getstring2 is incorrect. Because "Hello World" in getstring2 is a constant string located in the static storage zone, it remains unchanged during the lifetime of the program. No matter when getstring2 is called, it returns the same read-only memory block.
---------------------------------------------------------------
Let's take a look at Lin Rui's "High Quality C/C ++ programming". The above is very clear.