【发布时间】:2012-05-04 07:38:17
【问题描述】:
考虑这段代码(为简洁起见,删除了错误检查):
int main()
{
int fd, nread;
struct stat st_buff;
/* Get information about the file */
stat("data",&st_buff);
/* Open file data for reading */
char strbuff[st_buff.st_blksize];
fd = open("data",O_RDONLY);
/* read and write data */
do {
nread = read(fd,strbuff,st_buff.st_blksize);
if (!nread)
break;
write(STDOUT_FILENO, strbuff, nread);
} while (nread == st_buff.st_blksize);
/* close the file */
close(fd);
return 0;
}
这段代码在堆栈上为缓冲区分配内存(如果我没有误解的话。)还有alloca() 函数我可以用于相同目的(我猜)。我想知道我是否有任何理由要选择其中一个?
【问题讨论】:
标签: c arrays memory-management stack