How does the pointer Parameter Pass the memory ?, Pointer parameter transfer memory
If the function parameter is a pointer, do not expect this pointer to apply for dynamic memory.
The Test function's statement GetMemory (str, 200) 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
}
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, pointer
The copy of parameter p is _ p, and the compiler makes _ p = p. If the program in the function body modifies the content of _ p
Modify the content of parameter p. This is why pointers can be used as output parameters. In this example, _ p applies
The new memory is changed, but the memory address indicated by _ p is not changed at all. So the GetMemory Function
It cannot output anything. In fact, each execution of GetMemory exposes a piece of memory, because it is useless.
Free releases memory.
If you have to use the pointer parameter to request memory, you should use "pointer to Pointer" instead. See the example below:
Void GetMemory2 (char ** p, intnum)
{
* 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 );
}
Example7-4-2Apply for dynamic memory with pointer to 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.
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 );
}
Use function return values to pass dynamic memory
Although it is easy to use the function return value to transmit dynamic memoryReturnStatement Error
. Do not useReturnThe statement returns a pointer to the "stack memory" because the function ends.
Automatically disappears, as shown in the example.
Char * GetString (void)
{
Char p [] = "helloworld ";
Return p; // the compiler will give a warning
}
Void Test4 (void)
{
Char * str = NULL;
Str = GetString (); // str content is junk
Cout <str <endl;
}
ReturnStatement returns a pointer to the 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"Helloworld"It is garbage.
What if I rewrite the preceding example as follows?
Char * GetString2 (void)
{
Char * p = "helloworld ";
Return p;
}
Void Test5 (void)
{
Char * str = NULL;
Str = GetString2 ();
Cout <str <endl;
}
ReturnReturns a constant string.
Although the function Test5 runs without errors, the design concept of the function GetString2 is incorrect. Because
The "hello world" in GetString2 is a constant string located in the static storage zone. It is within the lifetime of the program.
Constant. No matter when GetString2 is called, it returns the same read-only memory block.