Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Skeleton Code: #include <iostream> #include <cstdlib> #include <cstring> using n

ID: 3674550 • Letter: S

Question

Skeleton Code:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;


// sum an array of numbers
int test1(int *data, int len)
{
int sum = 0;
for(int i=0; i <= len; i++){
sum += data[i];
}
return sum;
}

// Allocate a random number array
char* test2_init(int len)
{
char buf[80];
cout << "Enter a word: ";
cin >> buf;
char* mydat = new char[strlen(buf)+1];
strcpy(mydat, buf);
return mydat;
}

char* test2_reverse(char* word)
{
int len = strlen(word);
char* otherword = new char[len+1];
  
for(int i=0; i < strlen(word); i++){
otherword[i] = word[len-i-1];
}
otherword[len+1] = '';
delete [] word;
return otherword;
}

int main(int argc, char* argv[])
{
if(argc < 2){
cerr << "Please enter the test number you want to run [1-2]" << endl;
return 1;
}
const int len = 7;
int test = atoi(argv[1]);

if(test == 1){
// Test should sum up the array values and print it
int *data = new int[len];
for(int i=0; i < len; i++){
data[i] = rand()%100;
}
int sum = test1(data, len);
cout << "Test 1 sum is " << sum << endl;
}

else if(test == 2){
// Test should create a random string & then reverse a copy
char* myword = test2_init(len);
cout << "myword is " << myword << endl;

char* otherword = test2_reverse(myword);
cout << "otherword is " << otherword << endl;

delete [] myword;
delete [] otherword;
}
else {
cout << "Unknown test number" << endl;
}

return 0;
}

4.4 Fixing Valgrind Errors A program memleak.cpp is included with the files this week. It basically contains two different tests, both of which have bugs. Build and run it (your input in bold italics): $ ./compile memleak $ . /memleak 1 Test 1 sum is 475 $ . /memleak 2 Enter a word: rutabaga Both of these tests are wrong, but you will now fix them IMPORTANT: For all of the bugs you fix in this part of the lab, you have to keep some documentation of what bugs you find and what you change to fix them. This is mandatory because you will review with your CP/TA when you "check in." We recommend that you: prominently write comments in your code indicating each line you change and why you changed it keep a separate list somewhere of the chronological order of changes (was it for memleak 1 or memleak 2?) to guide your discussion with the CP/TA . 1. Run the 1st test: $ ./memleak1 This program is supposed to be adding up 7 random numbers between 0 and 99 and printing out the sum. However, now let's see if there are any hidden memory issues. Let's run valgrind: valgrind --tool-memcheck -1eak-check-yes ./memleak 1 You should see 2 errors (an Invalid Read and a block of memory that is definitely lost). An invalid read means you are trying to access a piece of memory that is not allocated to you. Usually it's because you have a bad pointer or access things beyond the end of an array. Use the line numbers in the error messages as clues and try to fix the code (Google the error message if you are having trouble understanding it) Recompile memleak. cpp and rerun valgrind until you have 0 errors

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;


// sum an array of numbers
int test1(int *data, int len)
{
int sum = 0;
   /** FIX: i < len so that we don't access unallocated memory **/
for(int i=0; i < len; i++){
    sum += data[i];
}
return sum;
}

// Allocate a random number array
char* test2_init(int len)
{
char buf[80];
cout << "Enter a word: ";
cin >> buf;
char* mydat = new char[strlen(buf)+1];
strcpy(mydat, buf);
return mydat;
}

char* test2_reverse(char* word)
{
int len = strlen(word);
char* otherword = new char[len+1];

for(int i = 0; i < strlen(word); i++){
    otherword[i] = word[len-i-1];
}
/** FIX: index starts from zero so last element would be at len for len + 1 array **/
otherword[len] = '';
delete [] word;
return otherword;
}

int main(int argc, char* argv[])
{
if(argc < 2){
    cerr << "Please enter the test number you want to run [1-2]" << endl;
    return 1;
}
const int len = 7;
int test = atoi(argv[1]);

if(test == 1){
    // Test should sum up the array values and print it
    int *data = new int[len];
    for(int i=0; i < len; i++){
      data[i] = rand()%100;
    }
    int sum = test1(data, len);
    cout << "Test 1 sum is " << sum << endl;

    /** FIX: clearing memory allocated to data array **/
    delete[] data;
}

else if(test == 2){
    // Test should create a random string & then reverse a copy
    char* myword = test2_init(len);
    cout << "myword is " << myword << endl;

    char* otherword = test2_reverse(myword);  
    cout << "otherword is " << otherword << endl;

    /** FIX: we are already deleting word in test2_reverse so need to delete here **/
    // delete [] myword;
    delete [] otherword;
}
else {
    cout << "Unknown test number" << endl;
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote