• 关于课堂上Exercise#1的讨论


    Software Testing_1b-rev page17

    Exercise#1

    代码内容:

    #include <stdio.h>
    int main(void)
    {
         char buff[10]; 
         memset(buff,0,sizeof(buff));
         gets(buff);
         printf(“
     The buffer entered is [%s]
    ”,buff);
         return 0;   
    }

    这段代码是否存在问题。。。

    据我所猜,老师布置的这个作业中没有存在代码的语法问题。那么,问题应该就存在于从数据层面上。

    gets()函数是从标准输入设备中读取字符串的函数,但是它可以无限读取,不会判断上限,以回车结束读取。所以,无法限制输入的字符数量的大小。儿buff被定义为一个容量大小只有10的字符数组,当输入的内容超过上界时,程序便会发生溢出。

    通过CodeBlocks进行了一下代码测试:(将代码转换成了C++,虽说这样很不科学,但是为了避免使用C语言时的不方便...嗯,你懂的!)

    代码如下:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
         char buff[10];
         memset(buff,0,sizeof(buff));
         gets(buff);
         cout<<"The buffer entered is "<<buff;
         return 0;
    
    }

    很明显,当输入的数据发生溢出时,程序被迫停止运行。

  • 相关阅读:
    Asp.Net Core, Angular 4 CLI站点部署
    .Net ViewState Workflow
    wpf附加属性理解
    wpf使用truetype字体ttf
    wpf 依赖属性注册解释
    wpf控件开发基础
    wpf path语法
    Adorner的收集
    正确理解WPF中的TemplatedParent (转贴)
    “/usr/local/var/run/nginx.pid” failed
  • 原文地址:https://www.cnblogs.com/ccvamy/p/4338029.html
Copyright © 2020-2023  润新知