GetMemory function parsing and getmemory function Parsing
GetMemory Function
Code 1:
void GetMemory(char *p)
{
p = (char*)malloc(100);
}
int main(int argc, char *argv[])
{
char *str = NULL;
GetMemory(str);
strcpy(str, "Hello");
return 0;
}
Str does not get the address value of the allocated memory.
Memory space status: first, four bytes of stack space are applied to store the str pointer. At this time, the str value is 0, and the address value of the memory used to store the str is 0x0012ff7c. Call the GetMemory function. The pointer P is written into the stack and four bytes of stack space are allocated. P is assigned the str value, that is, the P value is also 0, the memory address for storing pointer P is 0x0012ff2c. Then, the newly opened 100 bytes of memory space address is assigned to P, and the value of P is 0x00372b70.The str value is still 0 at the end of the function call., Str does not get the address value of the memory space of the 100 bytes!
Code 2:
void GetMemory(char **p)
{
*p = (char*)malloc(100);
}
int main(int argc, char *argv[])
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "Hello");
return 0;
}
Str to obtain the address value of the allocated memory.
Memory space status: first, four bytes of stack space are applied to store the str pointer. At this time, the str value is 0, and the address value of the memory used to store the str is 0x0012ff7c. Call the GetMemory function. The pointer P is added to the stack and four bytes of stack space are allocated. At this time, P is a second-level pointer, which stores the address value of the pointer str, that is, the value of P is 0x0012ff7c, the address of the memory space where the pointer P is stored is 0x0012ff2c. Then, the newly opened 100 bytes memory space address value 0x00372b70 is assigned to * P, that is, str, so the str value is 0x00372b70. The str value returned by the function is the address of the allocated 100 bytes of memory space!
Code 3:
Void GetMemory (char ** p)
{
// An error occurred while compiling this statement. A second-level pointer is directed to the allocated address.
// P = (char *) malloc (100 );
// You can use forced conversion, but the program crash
P = reinterpret_cast <char **> (malloc (100 ));
}
Int main (int argc, char * argv [])
{
Char * str = NULL;
GetMemory (& str );
Strcpy (str, "Hello ");
Return 0;
}
Str cannot get the address value of the allocated memory, the program crash.
If you use the following statement p = (char *) malloc (100) in the GetMemory function, a compilation error occurs becauseA second-level pointer cannot be directed to the allocated memory space address.. If force conversion p = reinterpret_cast <char **> (malloc (100) is used, compilation can be passed, but the program will crash.
Code 4:
Void GetMemory (char * p)
{
P = (char *) malloc (100 );
}
Int main (int argc, char * argv [])
{
Char * str = NULL;
GetMemory (& str); // This statement will cause compilation errors and pass a pointer address value to a level-1 pointer.
Strcpy (str, "Hello ");
Return 0;
}
Str cannot obtain the address value of the allocated memory. An error occurred while compiling.It is invalid to pass the address value of a pointer to a level-1 pointer!
Void GetMemory1 (char * p)
{
P = (char *) malloc (100 );
}
Void Test1 (void)
{
Char * str = NULL;
GetMemory1 (str );
Strcpy (str, "hello world ");
Printf (str );
}
// Str is always empty and the program crashes
Char * GetMemory2 (void)
{
Char p [] = "hello world ";
Return p;
}
Void Test2 (void)
{
Char * str = NULL;
Str = GetMemory2 ();
Printf (str );
}
Char * GetMemory3 (void)
{
Return "hello world ";
}
Void Test3 (void)
{
Char * str = NULL;
Str = GetMemory3 ();
Printf (str );
}
// Print hello world in Test3 because it returns the constant area and has not been modified. In Test2, the hello world cannot be printed, because the stack is pointed.
Void GetMemory4 (char ** p, int num)
{
* P = (char *) malloc (num );
}
Void Test4 (void)
{
Char * str = NULL;
GetMemory3 (& str, 100 );
Strcpy (str, "hello ");
Printf (str );
}
// The memory is not released.
Void Test5 (void)
{
Char * str = (char *) malloc (100 );
Strcpy (str, "hello ");
Free (str );
If (str! = NULL)
{
Strcpy (str, "world ");
Printf (str );
}
}
// Str is a wild pointer, and the printed result is unknown.
Void Test6 ()
{
Char * str = (char *) malloc (100 );
Strcpy (str, "hello ");
Str + = 6;
Free (str );
If (str! = NULL)
{
Strcpy (str, "world ");
Printf (str );
}
}