logo Hurry, Grab up to 30% discount on the entire course
Order Now logo

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Jesse RossResume writing
(5/5)

533 Answers

Hire Me
expert
Georgee BlackbridgeData mining
(5/5)

807 Answers

Hire Me
expert
joyComputer science
(4/5)

12 Answers

Hire Me
expert
Dan CurnowComputer science
(5/5)

938 Answers

Hire Me
C Programming

The following program contains five errors. Identify the errors and fix them

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

7COM1028

 

Secure Systems Programming

 

Referral Coursework: Secure Programming

(50%)

 

Please fill in your student ID

 

Student ID _____________________



(staff use only)

       
       
       
       
       
       
       

Total

     




There are 5 questions of 10 marks each.

 

Full marks may be obtained by attempting ALL questions.

 

Question1:

The following program contains five errors. Identify the errors and fix them.  (10 marks)

 


  1.   int main(void) {
  2. char source[10];
  3. char *dest;
  4. size_ti;

  1. strcpy(source, "0123456789");
  2. dest = malloc(strlen(source));
  3. for (i = 1; i <= 11; i++) {
  4. dest[i] = source [i];
  5. }
  6. dest[i] = '\0';
  7. printf("dest = %s", dest);

  1. return 0;
  2. }

 

Answer: 

 

In line 5 source size is 10 bytes long but we tries to copy 11 bytes , this error can be fixed by increasing the size of source variable to 11.

 

In line 6, strlen will not count the null byte this will allocate only 10 bytes and it can be fixed by using sizeof in place of strlen.

 

In line 7, i starts from 1 which is wrong as it should starts from 0.

 

In line 9, i terminates with value 11 but it should only loop through 10. It can be fixed by changing the terminating condition.

 

In line 10, i value will be 11 which is wrong as it can have maximum value of 10. It can be fixed by fixing the line 9.

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme

. This assignment exercises interfaces and generics. Be sure and read the assignment submission instructions provided previously. It gives you the content and format of what I expect you to post.

Assignment 8  This assignment exercises interfaces and generics. Be sure and read the assignment submission instructions provided previously. It