Void fun (char* s){
Char t[7];
S = t;
Strcpy(s,"example");
}
Int main(){
Char* s;
Fun(s);
Printf("%s",s);
Rentutn 0;
}
//输入字符串溢出空间,s指向为空.
Void test (int n){
For(unsigned int I = 0; n>=I;n--){
Printf("%d",n);
}
}
//死循环 -1的unsigned是一个很大的数
Void GetMemory1(char* p){
P = (char*)malloc(100);
}
Void Test1(void)
{
Char* str = NULL;
GetMemory1(str);
Strcpy(str,"hello world");
Printf(str);
}
//p在函数调用结束后会被释放,str为NULL,访问空指针 没有释放空间
Void GetMemory1(char** p){
*P = (char*)malloc(100);
}
Void Test1(void)
{
Char* str = NULL;
GetMemory1(&str);
Strcpy(str,"hello world");
Printf(str);
Free(str);
}
Void Test5(void)
{
Cahr* str = (char*)malloc(100);
Strcpy(str,"hello");
Free(str);
//str = NULL;
If(str! = NULL){
Strcpy(str,"world");
Printf(str);
}
}
//free把空间释放了,并不会对指针变量str赋值程NULL
//free之后堆区空间一点不要再次访问
//建议free之后,将指针变量赋值成NULL(str = NULL)
来自帅气博主的c语言文案
于 2022-08-24 18:57:36 首次发布