Git Product home page Git Product logo

assignment8's Introduction

Assignment 8

Objectives

To gain an understanding of memory allocation.

Part 1

  1. Write a program sort.c that dynamically allocates an array of integers. The size of the array (the number of integers, not the byte size) should be read from the user using scanf(). You may assume that the user will input a positive integer. The elements of the array should be filled using the rand() function. After filling the array with random numbers, your program should then make a copy of the array, and sort the new array in ascending order. Then make a second copy of the original array and sort it in descending order. Finally your program should print out all three arrays. All three arrays should be allocated using malloc() library function.

Part 2

In a text file assignment7.txt, answer the following questions:

Consider the following program, arg.c, shown with line numbers:

1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 int main(int argc, char **argv)
6 {
7   if (argc != 3) {
8     printf("usage: %s <arg1> <arg2>\n", argv[0]);
9     return 1;
10  }
11
12  char *a = *++argv;
13  char *b = *++argv;
14  char c[2] = { 0, 0 };
15  char *p;
16  char *q;
17
18  for (p = a; *p; p++) {
19     if (’a’ <= *p && *p <= ’z’)
20        *c = *p +A-a’;
21     else
22        *c = *p;
23    printf("%s", c);
24  }
25
26  p = (char *)malloc(strlen(a) + strlen(b) + 1);
27  strcpy(p, a);
28  q = p + strlen(a);
29  while ((*q++ = *b++) != 0)
30  ;
31  printf("%s\n", p);
32  p = NULL;
33  return 0;
34 }

It is compiled, linked and run with two command line arguments as follows:

$ gcc -g -Wall arg.c
$ ./a.out abc 123

Answer the following questions:

  1. What is the output? Assume that the malloc() call was successful. If you think that the program contains bugs (other than memory leaks) that can make the program crash (ex. segmentation fault), write "CAN CRASH" and then write the most likely output if the program happens to run without crashing.

  2. If the program is run using valgrind, how many bytes will valgrind report as "definitely lost"? You can write 0 if you think there is no memory leak that valgrind will consider "definitely lost". Please make sure to write the number of BYTES.

  3. Identify the line numbers that contains memory errors other than memory leaks (invalid access, for example.) If you think that there is no memory error other than possible memory leaks, write NONE. You don’t have to identify the nature of the memory errors. Just line numbers.

  4. Modify the program to fix ALL memory errors, including memory leaks. Write only the lines that need to be fixed. Write the line number and the line of code that will replace that line. If you think there is nothing to fix, write "NO CHANGE". The program can be memory error free by modifying 0 to 3 lines of code. Please do not modify more than 3 lines of code.

Submission Guidelines

Please include your name and a description in a comment at the top of your code files. Please also include your name at the top of your assignment7.txt file.

All files must be submitted via GitHub by 10:00am 7/12.

assignment8's People

Contributors

clesleycode avatar mnd2126 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.