博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C错题集锦
阅读量:4624 次
发布时间:2019-06-09

本文共 547 字,大约阅读时间需要 1 分钟。

1.野指针

#include
structstudent
{
char* name;//分配结构体内存时name未初始化,极易出错
int score;
}stu,*pstu;
int main()
{
pstu=(struct student*)malloc(sizeof(struct student));//隐含name野指针
strcpy(stu.name,"Jimy");//野指针,出错
stu.score=99;
return0;
} 2.按值传递 #include
#include
void GetMemory(char*p,int num)
{
//实际上只是让指针副本_str指向一块堆内存,正确做法可以用二级指针或者用返回值的方式
p=(char*)malloc(num*sizeof(char)); }
 
int main()
{
char* str=NULL;
GetMemory(str,10);//并没有分配到内存
strcpy(str,"hello");
free(str);//free并没有起作用,内存泄漏
return0;
 
}
 
 
 
 
 

转载于:https://www.cnblogs.com/encode/p/3550051.html

你可能感兴趣的文章
URI、URL 和 URN的区别
查看>>
根据表达式序列(前缀、中缀、后缀)构建表达式树
查看>>
mysql性能优化
查看>>
【SqlServer系列】语法定义符号解析
查看>>
Color Length UVA - 1625
查看>>
TLS/SSL
查看>>
zoj2319Beautiful People Dp
查看>>
图片加载 背景色块问题
查看>>
Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
查看>>
搭建git服务器
查看>>
iOS之UIDynamic UI动力学使用步骤
查看>>
poj 2498 动态规划
查看>>
Windows Phone 7中使用PhoneApplicationService类保存应用程序状态
查看>>
MySql数据库的下载和安装卸载
查看>>
JDBC接口核心的API
查看>>
双缓冲技术局部更新原理之派生自View
查看>>
PPAPI插件与浏览器的通信
查看>>
用 query 方法 获得xml 节点的值
查看>>
Hello,Android
查看>>
Sublime Text 3 build 3103 注册码
查看>>