|
void GetMemory (char *p)
{
p = (char *) malloc (100);
}
void Test (void)
{
char *str = NULL;
GetMemory (str);
value passing, one-way delivery, copy passing.
strcpy (str, "Hello World");
printf (str);
}
STR cannot obtain the memory space pointed to by P, and the change of parameter value does not affect the actual parameter value, and The heap space opened by malloc is not released at last.
|
Char *getmemory (void)
{
Char p[] = "Hello World";
return p;
P: array, stack space;
running char p[] = "Hello World" will open up 1 blocks of memory. Space and data are released after completion.
}
void Test (void)
{
char *str = NULL;
str = GetMemory ();
the space is released.
printf (str);
}
Str cannot get the memory space that p points to, and the child function returns a pointer to the stack space, which is reclaimed by the system after the function call ends, and the"Hello World" in the stack array space pointedto by P may have been destroyed by the system.
|
|
Void getmemory (char **p, int num)
{
*p = (char *) malloc (num);
}
void Test (void)
{
char *str = NULL;
GetMemory (&STR, 100);
strcpy (str, "Hello");
printf (str);
}
"Hello" can be printed, but the heap space created by malloc is not finally released.
|
void test (void)
{
char *str = (char *) malloc (+);
strcpy (str, "Hello");
free (str);
if (str! = NULL)
{
strcpy (str, "World");
// cannot manipulate heap space that has already been freed
printf (str);
}
}
// wrong in the heap space has been freed up and still using pointers to heap space. |
|
void Fun (char* str1, char* str2)
{
*str1 = *STR2;
}
main ()
{
char *str1= "abc\n";
char *str2= "bcd\n";
fun (str1, str2) ;//
printf (STR1);
Error modifying the contents of a character constant area by pointer |
void F1 (char *p)
{
p = (char *) malloc (+);
}
int test ()
{
char *str = NULL;
f1 (&STR);
strcpy (str, "Hello World");
printf (str);
}
//the argument type does not match and malloc The heap space that was opened is not finally released |