renaming
This commit is contained in:
committed by
JamesFlare1212
parent
2d2aaa903b
commit
f0e546a969
@@ -1,4 +1,4 @@
|
|||||||
# Lab 4 — Memory Diagrams and Memory Debugging
|
# Memory Diagrams and Memory Debugging
|
||||||
|
|
||||||
## Checkpoint 1
|
## Checkpoint 1
|
||||||
*estimate: 30-45 minutes*
|
*estimate: 30-45 minutes*
|
||||||
@@ -31,7 +31,7 @@ Read lecture notes [6.7](../../lectures/06_memory#67-memory-debugging), [6.8](..
|
|||||||
|
|
||||||
Please download the following 4 files needed for this lab:
|
Please download the following 4 files needed for this lab:
|
||||||
|
|
||||||
[buggy_lab4.cpp](./buggy_lab4.cpp)
|
[buggy_lab.cpp](./buggy_lab.cpp)
|
||||||
[first.txt](./first.txt)
|
[first.txt](./first.txt)
|
||||||
[middle.txt](./middle.txt)
|
[middle.txt](./middle.txt)
|
||||||
[last.txt](./last.txt)
|
[last.txt](./last.txt)
|
||||||
@@ -39,7 +39,7 @@ Please download the following 4 files needed for this lab:
|
|||||||
## Checkpoint 2
|
## Checkpoint 2
|
||||||
*estimate: 20-40 minutes*
|
*estimate: 20-40 minutes*
|
||||||
|
|
||||||
For Checkpoint 2 of this lab, we will heavily rely on dynamic memory to find the average and smallest number for a set of data from an input file. You will use a memory debugging tool such as DrMemory or Valgrind to fix memory errors and leaks in buggy_lab4.cpp. Make sure to download the provided .txt files as well.
|
For Checkpoint 2 of this lab, we will heavily rely on dynamic memory to find the average and smallest number for a set of data from an input file. You will use a memory debugging tool such as DrMemory or Valgrind to fix memory errors and leaks in buggy_lab.cpp. Make sure to download the provided .txt files as well.
|
||||||
First take a look at the code inside the identifyMeanAndMin() function. You will notice that the syntax used
|
First take a look at the code inside the identifyMeanAndMin() function. You will notice that the syntax used
|
||||||
throughout the program may be a little different than what you are used to. Try and familiarize yourself with
|
throughout the program may be a little different than what you are used to. Try and familiarize yourself with
|
||||||
this syntax before you start working on the assignment. What does this line of code do?
|
this syntax before you start working on the assignment. What does this line of code do?
|
||||||
@@ -52,13 +52,13 @@ Whats going on inside the *for* loop? If you are stuck on this, ask a mentor or
|
|||||||
soon as possible and refer back to your lecture notes on pointers and arrays.
|
soon as possible and refer back to your lecture notes on pointers and arrays.
|
||||||
Once you have done this, compile the program using:
|
Once you have done this, compile the program using:
|
||||||
```console
|
```console
|
||||||
g++ buggy_lab4.cpp -o buggy_lab4.out -g -Wall
|
g++ buggy_lab.cpp -o buggy_lab.out -g -Wall
|
||||||
```
|
```
|
||||||
|
|
||||||
Try running the program normally using:
|
Try running the program normally using:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
./buggy_lab4.out
|
./buggy_lab.out
|
||||||
```
|
```
|
||||||
|
|
||||||
You will notice that a segmentation fault occurs. Now run this program using either Valgrind or DrMemory. If running using Valgrind, remember to use --leak-check=yes or --leak-check=full.
|
You will notice that a segmentation fault occurs. Now run this program using either Valgrind or DrMemory. If running using Valgrind, remember to use --leak-check=yes or --leak-check=full.
|
||||||
@@ -67,11 +67,11 @@ Your memory debugger should give you more context as to why a segmentation fault
|
|||||||
|
|
||||||
It is highly recommended that you tackle one part at a time. For example, after adding a few lines of code to part 1, you will now receive different memory errors when you recompile and run the program using your memory debugger. Similarly, fixing all memory errors in part 2 will generate different memory errors that should be resolved in part 3.
|
It is highly recommended that you tackle one part at a time. For example, after adding a few lines of code to part 1, you will now receive different memory errors when you recompile and run the program using your memory debugger. Similarly, fixing all memory errors in part 2 will generate different memory errors that should be resolved in part 3.
|
||||||
|
|
||||||
In part 2 of buggy_lab4.cpp, the goal is to print out the contents of intArray in reverse order, while also calculating the sum of all elements in the array and keeping track of the smallest number encountered. Solutions that attempt to print the contents of the array in a different manner or end up with the wrong
|
In part 2 of buggy_lab.cpp, the goal is to print out the contents of intArray in reverse order, while also calculating the sum of all elements in the array and keeping track of the smallest number encountered. Solutions that attempt to print the contents of the array in a different manner or end up with the wrong
|
||||||
value for the smallest number found or sum won't be accepted.
|
value for the smallest number found or sum won't be accepted.
|
||||||
Note that:
|
Note that:
|
||||||
- You are only allowed to modify or add code when asked to. This would be between the comments that
|
- You are only allowed to modify or add code when asked to. This would be between the comments that
|
||||||
indicate parts 1, 2, and 3 inside buggy_lab4.cpp. Do not modify other parts of the code or create any
|
indicate parts 1, 2, and 3 inside buggy_lab.cpp. Do not modify other parts of the code or create any
|
||||||
helper functions.
|
helper functions.
|
||||||
- You are not allowed to declare new variables; the ones provided are more than enough. Hint: how do
|
- You are not allowed to declare new variables; the ones provided are more than enough. Hint: how do
|
||||||
we create memory on the heap?
|
we create memory on the heap?
|
||||||
@@ -79,9 +79,9 @@ we create memory on the heap?
|
|||||||
|
|
||||||
**You will receive no credit if you do not follow the above restrictions.**
|
**You will receive no credit if you do not follow the above restrictions.**
|
||||||
|
|
||||||
**To receive credit for this checkpoint**: Fix buggy_lab4.cpp so that it successfully prints out the average
|
**To receive credit for this checkpoint**: Fix buggy_lab.cpp so that it successfully prints out the average
|
||||||
and smallest number for a given set of data and is free of all memory errors and leaks on your local machine.
|
and smallest number for a given set of data and is free of all memory errors and leaks on your local machine.
|
||||||
Submit buggy_lab4.cpp on Submitty and verify that there are no memory errors there as well and show a
|
Submit buggy_lab.cpp on Submitty and verify that there are no memory errors there as well and show a
|
||||||
mentor or TA both results. Also explain to a mentor TA what you added or modified in the program to
|
mentor or TA both results. Also explain to a mentor TA what you added or modified in the program to
|
||||||
resolve all memory errors.
|
resolve all memory errors.
|
||||||
|
|
||||||
|
|||||||
116
labs/memory_debugging/buggy_lab.cpp
Normal file
116
labs/memory_debugging/buggy_lab.cpp
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#define MAX_ARRAY_SIZE 50
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** IMPORTANT: SOLUTIONS THAT DO NOT FOLLOW BELOW INSTRUCTIONS WILL RECEIVE NO CREDIT
|
||||||
|
* Do not add or modify code in this file UNLESS ASKED TO!
|
||||||
|
* You are NOT allowed to declare new variables; use the ones already declared.
|
||||||
|
* You are NOT allowed to create new helper functions or modify checkCorrectSmallest()
|
||||||
|
**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** DO NOT MODIFY THIS FUNCTION! **/
|
||||||
|
int checkCorrectSmallest(int* smallestNum, const std::string& filename) {
|
||||||
|
if (filename == "first.txt" || filename == "middle.txt") {
|
||||||
|
if (*smallestNum != 1) {return -1;}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (filename == "last.txt") {
|
||||||
|
if (*smallestNum != 22) {return -1;}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A function that will identify the mean and smallest number
|
||||||
|
* in a set of data provided that there are at most 50 numbers
|
||||||
|
* in the file.
|
||||||
|
*
|
||||||
|
* @param filename: The name of a file that contains a list of numbers.
|
||||||
|
*
|
||||||
|
* Task: Add or modify code in the appropriate sections to fix memory and logic errors
|
||||||
|
* without using data structures (such as an array or vector) and without using any
|
||||||
|
* additional memory on the stack.
|
||||||
|
**/
|
||||||
|
|
||||||
|
void identifyMeanAndMin(const std::string& filename) {
|
||||||
|
int* numElements;
|
||||||
|
int* sum;
|
||||||
|
int* smallestNum;
|
||||||
|
float* avg;
|
||||||
|
|
||||||
|
/** PART 1: ADD CODE BELOW **/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** PART 1: ADD CODE ABOVE **/
|
||||||
|
|
||||||
|
*numElements = 0;
|
||||||
|
*sum = 0;
|
||||||
|
|
||||||
|
int readInt;
|
||||||
|
int* intArray = new int[MAX_ARRAY_SIZE];
|
||||||
|
std::ifstream input(filename);
|
||||||
|
|
||||||
|
while (input >> readInt) {
|
||||||
|
*(intArray + *numElements) = readInt;
|
||||||
|
*numElements += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Printing the contents of the array in reverse order: ";
|
||||||
|
|
||||||
|
/** PART 2: MODIFY CODE BELOW **/
|
||||||
|
|
||||||
|
for (int i = MAX_ARRAY_SIZE; i >= -1; i--) {
|
||||||
|
// If we're at the beginning of the for loop, initalize *smallestNum
|
||||||
|
// Else, compare *smallestNum to current element in the for loop
|
||||||
|
if (i == MAX_ARRAY_SIZE) {
|
||||||
|
*smallestNum = *(intArray + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
if (*smallestNum > *(intArray + i)) {
|
||||||
|
*smallestNum = *(intArray + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** PART 2: MODIFY CODE ABOVE **/
|
||||||
|
|
||||||
|
*sum += *(intArray + i);
|
||||||
|
std::cout << *(intArray + i) << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
if (checkCorrectSmallest(smallestNum, filename) == -1) {
|
||||||
|
std::cout << "ERROR: incorrect value for smallest number" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
*avg = *sum / float(*numElements);
|
||||||
|
std::cout << "The average for the set of numbers in " << filename << " is "
|
||||||
|
<< *avg << " and the smallest number is " << *smallestNum
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
/** PART 3: ADD AND/OR MODIFY CODE BELOW **/
|
||||||
|
|
||||||
|
delete intArray;
|
||||||
|
|
||||||
|
/** PART 3: ADD AND/OR MODIFY CODE ABOVE **/
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
identifyMeanAndMin("first.txt");
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
identifyMeanAndMin("middle.txt");
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
identifyMeanAndMin("last.txt");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user