char* buf; buf=malloc(10);这样非法吗?

[复制链接]
查看11 | 回复2 | 2010-7-2 10:21:43 | 显示全部楼层 |阅读模式
在网上一段代码:
struct mtop mt;
long long total = 0;
char
*rbuf;
int
rec_count = 0;
int
rdfd;
int
bytes, count;
int
rc = 0;
// printf("Opening destination file %s...\n", fname);
rdfd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666);
if(rdfd == -1)

return -2;
/* get max_blksize and max_scsi_xfer in parms */
rc = stiocqryp();
if(rc != 0)

{

//close(rdfd);

//return rc;

}
//blksize = min(parms.max_blksize, parms.max_scsi_xfer);
// blksize = min(blksize, MAX_TRANSFER_SIZE);
//blksize=MAX_TRANSFER_SIZE;
//blksize=131072;
/* set actual block size to variable */
mt.mt_op = MTSETBLK;
mt.mt_count = 0;
//printf("Setting block size to variable...\n");
rc = ioctl(fd, MTIOCTOP, &mt);
if(rc != 0)
{
printf("Errot MTIOCTOP!\n");

close(rdfd);

return rc;
}
rbuf = malloc(blksize);
上面这句就是直接定义的编译无错,但我这么定义就有错?

回复

使用道具 举报

千问 | 2010-7-2 10:21:43 | 显示全部楼层
1、没看到blksize定义和赋值;2、最好强转一下类型,否则有些编译器可能过不去;rbuf=(char *)malloc(blksize);
回复

使用道具 举报

千问 | 2010-7-2 10:21:43 | 显示全部楼层
char* buf; buf=malloc(10);相当于buf[10];不过malloc返回的是void *,建议转换下指针类型,如:char* buf; buf=(char*)malloc(10);
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行