How to transmit memory using pointer Parameters

Source: Internet
Author: User

Vczh 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 exposes 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" is not easy to understand, we can use function return values to pass 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. Here, we do not use the return statement 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.

The original text is as follows:
The program in the function body modifies the content of _ p and the memory address indicated by _ p. the previous sentence refers to modifying the content in the address referred to by _ p, And the next sentence refers to changing the address.

In test


Str is a pointer variable. Suppose its address is 1111 h, and its value is NULL (0000 h)
In getmemory (str, 100), the str value is 0000 h.

In GetMemory, p is a pointer to a character (in the function's temporary stack). Assume that its address is 1000 h,
If the input and output values in the function call are 0000 h, the value in the Variable p is 0000 h.
After p = (char *) malloc (sizeof (char) * num) is executed, the value of p is changed to the allocated heap memory, for example, 1234 H.

When a function is returned, p is invalid because it is in the function stack and ends with the function.

The value in str is still 0000 h [positive solution !]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.