Parameter policy
If the parameter of the function is a pointer, do not expect to use the pointer to dynamically request memory. As follows:
void getmemory (char *p, int num) {p = (char * ) malloc (sizeof ( char ) * num);} void Test (void char *str = 100 ); // STR is still not null strcpy (str, " hello " ); // run error }
The reason is that the compiler always makes a temporary copy of each parameter. The pointer parameter p, whose copy is _p, makes the _p=p. If you change what _p refers to, the content of the corresponding p refers to the change (pointing to the same place, after all). However, the dynamic allocation of memory space in GetMemory changes the contents of _p. The p in the calling function is still pointing to null. In addition, because the function getmemory in the dynamic allocation of space, but not released, so call a function, the leak once memory. Icon:
If you have to request memory with a pointer parameter, you can request memory with the pointer's pointer as a parameter
voidGetMemory (Char**p,intnum) { *p = (Char*)malloc(sizeof(Char) *num);}voidTest (void){ Char*str =NULL; GetMemory (&STR, -);//Remember to add address charactersstrcpy (str,"Hello"); Free(str)}
The principle is the same, more difficult to understand, the diagram shows:
The better way is to pass a pointer reference
#include <iostream>#include<string>#include<cstring>#include<cstdlib>using namespacestd;voidGetMemory (Char*&p,intnum) {P= (Char*)malloc(sizeof(Char) *num);}voidTest (void){ Char*str =NULL; GetMemory (str, -); strcpy (str,"Hello"); cout<< Str <<Endl; Free(str);}intMain () {Test ();}
Note here that the reference to the pointer is char* &a, if it is not possible to understand:
Char*&a
return value Policy
You can use the function return value to pass dynamic memory. This method is much simpler than the pointer pointer.
Char*getmemory (intnum) { Char*p = (Char*)malloc(sizeof(Char) *num); returnp;}voidTest (void){ Char*str =NULL; STR= GetMemory ( -);//str points to a dynamically allocated spacestrcpy (str,"Hello"); Free(str)}
When using the return value, do not return a pointer to "stack memory", reference, because the memory automatically dies at the end of the function, the returned pointer is a wild pointer. For example
Char *GetString () { char"helloWorld"// The contents of the array are stored in the stack, and when the function ends, the return P is released; void Test (void) { char *str = NULL; = GetString (); // because the non-matching memory has already been released, at this time str is a wild pointer, the content is garbage cout << str << Endl;}
Do not define an array in the function, define pointers, example:
Char *GetString () { char"helloWorld"// The contents of the array are stored in the static area, and when the function ends, the return p is not released ; void Test (void) { char *str = NULL; = GetString (); << str << Endl;}
The program is correct at this point, but there is a point where the allocated memory is in the static zone and can only be read but not modifiable .
Original path: http://www.cnblogs.com/kaituorensheng/p/3246900.html
Go to the C + + pointer parameter is how the memory is passed