Getmemory经典
来源:互联网 发布:六扇门调查知乎 编辑:程序博客网 时间:2024/06/09 23:00
void GetMemory1(char *p)
{
p = (char *)malloc(100);
}
void Test1(void)
{
char *str = NULL;
GetMemory1(str);
strcpy(str, "hello world");
printf(str);
}
//str一直是空,程序崩溃
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);
}
//Test3 中打印hello world,因为返回常量区,而且并没有被修改过。Test2中不一定能打印出hello world,因为指向的是栈。
void GetMemory4(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test4(void)
{
char *str = NULL;
GetMemory3(&str, 100);
strcpy(str, "hello");
printf(str);
}
//内存没释放
void Test5(void)
{
char *str = (char *) malloc(100);
strcpy(str, "hello");
free(str);
if(str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
//str为野指针,打印的结果不得而知
void Test6()
{
char *str=(char *)malloc(100);
strcpy(str, "hello");
str+=6;
free(str);
if(str!=NULL)
{
strcpy(str, "world");
printf(str);
}
}
//VC断言失败,运行错误
- Getmemory经典
- GetMemory函数的经典错误
- GetMemory
- GetMemory
- GetMemory()
- GetMemory
- GetMemory()
- GetMemory
- Getmemory的几个经典的关于内存的笔试题
- GetMemory函数的几种经典考法(转)
- GetMemory函数的几种经典考法
- GetMemory函数的几种经典考法
- GetMemory函数的几种经典考法
- GetMemory函数的几种经典考法
- GetMemory函数的几种经典考法
- GetMemory函数的几种经典考法(转)
- GetMemory函数的几种经典考法
- GetMemory函数的几种经典考法
- 多项式回归
- 单点登录(SSO)的实现—通行证的基本原理
- 进销存练习前台界面完成
- Hibernate中的cascade和inverse
- 一家软件公司上机面试题目c++程序员
- Getmemory经典
- 韩少功:怎么赚钱
- Mysql 的 Cascade Restrict
- sql语句总结
- asp.net 成员角色管理
- mysql 数据库表的类型
- Type=MyISAM CHARACTER SET utf8 解说
- 按钮 运行代码 复制代码 保存代码
- 中国IT技术论坛 —— mysql板块