A discussion on exercise#1 in class

Source: Internet
Author: User

Software Testing_1b-rev Page17

Exercise#1

Code content:

#include <stdio.h>int main (void) {     char buff[];      memset (Buff,0,sizeof(buff));     Gets (buff);       is [%s]\n ", buff);      return 0 ;   }

Is there a problem with this piece of code ...

As far as I can guess, there is no grammatical problem with the code in this job assigned by the teacher. So, the problem should be at the data level.

The Get () function is a function that reads a string from a standard input device, but it can read indefinitely without judging the upper bound to end the read with a carriage return. Therefore, you cannot limit the size of the number of characters you enter. A child buff is defined as an array of characters with a capacity of only 10, and when the input exceeds the upper bound, the program overflows.

A bit of code testing was done through Codeblocks: (converting code to C + +, although this is not scientific, but in order to avoid the use of C language inconvenient ...) Well, you know! )

The code is as follows:

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intMain () {Charbuff[Ten]; memset (Buff,0,sizeof(Buff));     Gets (buff); cout<<"The buffer entered is"<<Buff; return 0;}

Obviously, when the input data overflows, the program is forced to stop running.

A discussion on exercise#1 in class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.