免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1555 | 回复: 3
打印 上一主题 下一主题

bad address [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-13 20:05 |只看该作者 |倒序浏览
下面程序在linux fc7 下用gcc编译
#include<stdio.h>
#include<errno.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
main(int argc,char * argv[] )
{
int fd1,fd2,l;
void * tp;
char *path;
char buf[50];
struct stat ps;
path=buf;
printf("enter a path:");
scanf("%s",path);
printf("%s",argv[1]);
fstat(fd1.&ps);
if(fd1=(open(argv[1],O_RDONLY))==-1) {printf("error1");exit(1);}
if(fd2=(open(path,O_CREAT|O_WRONLY))==-1) {printf("error2");exit(1);}
l=ps.st_size;
tp=malloc(l);
if (tp==NULL) {printf("erroe_7");}
if(read(fd1,tp,l)!=l) {perror("read error3");exit(1);}
write(fd2,tp,l);
if(close(fd1)==-1||close(fd2)==-1)
{printf("error4");exit(1);}
}

错误情况
alhost jun]# gcc aa.c
[root@localhost jun]# ./a.out /jun/41
enter a path:/jun/63
read error3: Bad address
/jun/41error_[root@localhost jun]#
请问这个那里错误了

[ 本帖最后由 shiyangb 于 2007-12-13 21:59 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-12-13 21:10 |只看该作者
tp=malloc(l);

论坛徽章:
0
3 [报告]
发表于 2007-12-13 21:12 |只看该作者
ps.st_size 没有初始化吧。

应该是这样用吧
ps.st_size = sizeof( ps );
read(fd1, &ps );

论坛徽章:
0
4 [报告]
发表于 2007-12-13 21:16 |只看该作者
l=ps.st_size;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP