Parameter Policy
If the function parameter is a pointer, do not expect this pointer to dynamically apply for memory. As follows:
void GetMemory(char *p, int num)
{ p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{ char *str = NULL;
Getmemory (STR, 100); // STR is not null
Strcpy (STR, "hello"); // running error
}
The reason is that the compiler always creates a temporary copy for each parameter. Pointer parameter P. Its copy is _ p, so that _ p = P. If the content referred to by _ p is changed, the content referred to by corresponding p also changes (points to the same place after all ). However, in getmemory, the memory space is dynamically allocated, which changes the content of _ p. In the call function, P still points to null. Furthermore, because the function getmemory dynamically allocates space but does not release it, calling the memory function exposes the memory. Figure:
If you have to use the pointer parameter to apply for memory, you can use the pointer as the parameter to apply for memory.
void GetMemory(char **p, int num)
{ *p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{ char *str = NULL;
Getmemory (& STR, 100); // remember to add the address character strcpy (STR, "hello"); free (STR)
}
The principle is the same, which is hard to understand:
The better way isPointer Reference
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
void GetMemory(char *&p, int num)
{ p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{ char *str = NULL;
GetMemory(str, 100);
strcpy(str, "hello");
cout << str << endl;
free(str);
}
int main()
{ Test();
}
Note that the pointer is referenced as char * & A. If it is hard to understand, it can be as follows:
typedef char* pchar;
pchar &a
Return Value Policy
You can use the function return value to transmit dynamic memory. This method is much simpler than the "Pointer" method.
char *GetMemory(int num)
{ char *p = (char *)malloc(sizeof(char) * num);
return p;
}
void Test(void)
{ char *str = NULL;
STR = getmemory (100); // STR points to the dynamically allocated space
strcpy(str, "hello");
free(str)
}
When using the return value, never return a pointer or reference pointing to the "stack memory", because the internal function automatically disappears when it ends, and the returned pointer is a wild pointer. For example
char *GetString()
{Char P [] = "Hello World"; // The array content is stored in the stack. When the function ends, it is released.
return p;
}
void Test(void)
{ char *str = NULL;
STR = getstring (); // because the out-of-configuration memory has been released, STR is a wild pointer and the content is junk.
cout << str << endl;
}
Define a pointer instead of an array in a function. For example:
char *GetString()
{Char * P = "Hello World"; // The array content is stored in the static zone and will not be released when the function ends.
return p;
}
void Test(void)
{ char *str = NULL;
str = GetString();
cout << str << endl;
}
At this time, the program is correct, but one thing is that the allocated memory is in the static zone and can only be readUnchangeable.
Reprinted from: http://www.cnblogs.com/kaituorensheng/p/3246900.html
How pointer parameters transmit memory (reprinted)