次のコードを検討してください (簡潔にするためにエラー チェックは削除されています)。
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()
)。同じ目的で使用できた関数もあります (推測します)。どちらかを選択したい理由があるかどうか疑問に思っていましたか?