考虑以下代码的可变长度阵列(误差为简洁检查删除):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