/* Demonstration of buffer overflow */
#include <stdio.h>
#include <stdlib.h>
/* Implementation of library function gets() */
char *gets(char *dest)
{
int c = getchar();
char *p = dest;
while (c != EOF && c != '\n') {
*p++ = c;
c = getchar();
}
*p = '\0';
return dest;
}
/* Read input line and write it back */
void echo()
{
char buf[4]; /* Way too small! */
gets(buf);
puts(buf);
}
void call_echo()
{
echo();
}
/*void smash()
{
printf("I've been smashed!\n");
exit(0);
}
*/
int main()
{
printf("Type a string:");
call_echo();
return 0;
}
Bufdemo程序
程序分析:挨个输入字符,放入长度为4 的数组中,再逐个输出,若输入的字符超过4,则停止运行
call_echo(),递归函数可以有栈缓存,输入数字也可以,数字和字符在计算机存储内容不一样,运行规律 不一样,所 以输出可能不一样