考慮以下代碼的可變長度陣列(誤差爲簡潔檢查刪除):ALLOCA超過用於字符緩衝器
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()
函數,我可以用於相同的目的(我猜)。我想知道爲什麼我會選擇一個而不是其他的理由?
我不知道malloca,但我想這是Windows的東西吧?我的圖書館似乎沒有找到malloca。 – yasar 2012-04-23 17:21:43