- The return statement cannot return "Pointer" or "Reference" pointing to "stack memory" because the function body is automatically destroyed when it ends.
Char * func (void)
{
Char STR [] = "Hellow world ";
// STR memory is on the stack
Return STR; // Error
}
Void test4 (void)
{
Char * STR = NULL;
STR = getstring ();
// STR content is Spam
Cout <STR <Endl;
}
Although STR points to the address of "Hellow world" in the func function, when the function ends, the content pointed to by the pointer has been destroyed.
Char * getmemory (INT num)
{
Char * P = (char *) malloc (sizeof (char) * num );
Return P;
}
Void main (void)
{
Char * STR = NULL;
STR = getmemory (100 );
Strcpy (STR, "Hello world! ");
Cout <STR <Endl;
Free (STR); STR = NULL;
}
In this example, p is the heap memory pointer. In C, the memory allocated through malloc is directed to the heap. The memory dynamically applied by malloc or new is the memory on the heap.
- How to transmit pointer parameters to memory
If the function parameter is a pointer, do not expect this pointer to apply for dynamic memory. In example 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 nullstrcpy (STR, "hello ");
// Running error
}
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, every execution of getmemory
A piece of memory will be leaked because the free memory is not released. If you have to use the pointer parameter to request memory, you should use "pointer to Pointer" instead ",
See example 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 strstrcpy (STR, "hello ");
Cout <STR <Endl;
Free (STR );
}
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 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 3 using the function return value to pass the dynamic memory is easy to use, but some people often use the return statement with an error
. 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 at the end, as shown in Example 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 Spam
Cout <STR <Endl;
}
In Example 4, the Return Statement returns the pointer pointing to "stack memory" and uses the debugger to track test4 step by step. After executing the STR = getstring statement, STR is no longer a null pointer, however, STR content is not "Hello World" but garbage.
What if I rewrite Example 4 to Example 5?
/* = = */
Char * getstring2 (void)
{
Char * P = "Hello World ";
Return P;
}
Void test5 (void)
{
Char * STR = NULL;
STR = getstring2 ();
Cout <STR <Endl;
}
In Example 5, the Return Statement returns a constant string. Although the test5 function does not run properly, the design concept of the getstring2 function 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.