task description
In this exercise, you will learn about the Memory Check Utility.door of your choice
.
Valgrind helps you identify memory errors and practice implementing the big three. Valgrind is an extremely useful tool for debugging memory problems and repairing thread errors or other failures. This lab is also particularly important becauseWe will check your mappings for errors and memory leaks. You lose points for memory leaks and/or memory errors (we also teach the difference between a memory leak and a memory error). You must verify your code with Valgrind before submitting it. You should also be aware that Valgrind will only detect a leak if your program allocates memory and does not deallocate it. It cannot find a leak unless the code containing the leak is executed when the program runs. Therefore, you must test your code thoroughly and verify these tests with Valgrind.
job outlook
As you can see from the description, this lab focuses heavily on memory management and best practices for dealing with memory management issues such as: B. Memory leaks. (You can update what memory leak meanson here). It is even possible to create lightweight tools similar to Valgrind, where these tools can track the memory used by the program and report information about memory management issues such as memory leaks. If this lab interests you, CS 341 is a course that covers how to write memory allocation models and develop tools that are functionally similar to Valgrind.
code description
For this internship, you will correct errors in the program for assigning students to lecture halls. Because many CS classes are very large (CS 225 has almost 1,200 students!), exams are often spread across multiple rooms. Before each exam, course staff should assign different rooms to different students so that everyone with enough space can take the exam.
For example, if there are only two classrooms of the same size, first semester students (last name letters A–N) could go to Siebel 1404, while second semester students (letters M–Z) could go to Siebel 1404. Siebel 1404 . they could go to Siebel 1404. go to DCL 1320.
However, with more rooms, this problem becomes more difficult. In the given example situation, there are 9 test rooms with a capacity of 43 to 70 seats (ie 21 to 35 students sitting at alternating desks). Even though we need to break the alphabet even further, I still like to put students with the same first letter of their last name in the same class because it makes it easier to find the correct class.
We have provided you with code to fix this problem, but it contains several memory errors. You need to use Valgrind and some debugging skills from lab_debug to find and fix the errors. Observation,there are no errors in itarchive
namespace.
A reference to the laboratory can be found atoxygenthe image.
code review
All tasks will be distributed through our release github repository this semester. You must have your git directory configured to have our version as a remote repository as described inuse Git-Setup
You can merge tasks as soon as they are published to your personal repository
git pull --no-edit --no-rebase release maingit push
If you use multiple computers, you may need to use the following for them to work properly.
git pull --no-edit --no-rebase Release Principal --allow-unrelated-historiesgit push
The first Git command finds and applies changes to thedirector
branch in your remote repository namedstart
on your team. Him--no edition
flag automatically generates a confirmation message for you, and the--no-rebase
flag will merge the upstream branch into the current branch. In general, these two flags should not be used, but they are included to make it easier to combine tasks in your repository.
The second command pushesorigin
(your employees), which allows you to keep up with new changesstart
.
You must run these commands for every published job.
All files for this lab are onlaboratory_storage
Directory.
prepare your code
This semester, for parliamentarians, we use CMake instead of just make. This allows us to use libraries like Catch2 that can be installed on your system instead of being shipped with each task. This change means that you must use CMake for each task to create your own custom makefiles. To do this, you need to run the following in the task's home directory. Which one in this task is thelaboratory_storage
Directory.
build mkdir buildcd
This will first create a new directory in your mapping directory calledbuild
. Here you create the actual task and then switch to that directory. This is not included in the provided code as we follow industry best practices and typically exclude the build directory of any source control system.
Now you need to run CMake as follows.
tb..
This will run CMake to initialize the current directory containing thebuild
Directory you just created as the location to create the task. The only argument to CMake here is..
which refers to the parent directory of the current directory, which in this case is the top of the map. This directory contains the files that CMake needs to configure your mapping to be created.
At this point, you can run make in the build directory as described to compile the various programs into the MP.
Valgrind's background
Valgrind is a useful tool for detecting memory errors and memory leaks.
Valgrind is a free memory debugging, memory leak detection and profiling utility. It only runs on Linux systems. To prepare your project for Valgrind to examine, you need to compile and link it using the debug options.- Grama
j-O0
. Get yoursMakefile
You use these options when compiling. To test your program with Valgrind you need to use the following command:
valgrind ./program
To tell valgrind to also check for memory leaks, run:
door of your choice- Check for leaks=vol./software
You will see a report of any errors or inconsistencies found. Each line in the report begins with the process ID (the process ID is a number that the operating system assigns to a running program). Each error has a description, a stack trace (showing where the error occurred), and other data about the error. It is important to remove bugs in the order in which they occur during runtime, as a single initial bug can cause more later on.
Here is a list of some errors that Valgrind can detect and report. (Note that not all of these bugs are present in the practice code.)
- Invalid read/write errors.This error occurs when your code reads or writes a memory address that you have not allocated. This error sometimes occurs when an array is indexed beyond its limit, known as an "overflow" error. Unfortunately, Valgrind cannot search locally allocated arrays (i.e. those on the stack). The overflow check is performed for dynamic memory only.
example
e T * Arr = neu e T[6];Arr[10] = -5;
- Using an uninitialized value.This type of error occurs when your code uses a declared variable before making an explicit assignment to the variable.
example
e T x;cout << x << Final;
- Invalid error free.This occurs when your code tries to evict memory allocated twice or evict memory that was not allocated
neu
.
example
e T * x = neu e T;removed x;removed x;
- incompatible
free()/remover/remover[]
.Valgrind tracks the method your code uses when allocating memory. If it is deallocated using any other method, you will be notified of the error.
example
e T * x = neu e T[6];removed x;
- Memory leak detection.Valgrind can detect three sources of memory leaks.
- ANBlock still accessibleThis happens when you forget to delete an object, the pointer to the object still exists, and the memory for the object is still allocated.
- ANmissing blockIt is a little complicated. A pointer to a part of the memory block still exists, but it's not clear whether it points to the block or is independent of it.
- ANdefinitely missing blockoccurs when the block is not removed, but no pointer to it is found.
example
e T * x = neu e T[6]; // without removing the corresponding x
For more information on the Valgrind utility, see the links below:
- http://www.valgrind.org/docs/manual/quick-start.html
- http://www.valgrind.org/docs/manual/faq.html#faq.reports
- http://www.valgrind.org/docs/manual/manual.html
Memory bug fixed (with Valgrind)
Before fixing the errors, you need to compile the code:
again
This will create an executable called Storage that you can run:
./Memory
You can then run Valgrind in memory:
Valgrind ./Memories
This works fine to fix memory errors but to fix memory leaks you need to add--leak-check=lleno
Before./Memory
:
door of your choice- Check for leaks=full ./memory
After fixing any Valgrind errors, you can test your program's output with:
./Memory>salida.txtdiff salida.txt ../data/soln_output.txt
Note that most of the work in this exercise is fixing valgrind errors and memory leaks, not the program output, which should be correct once the memory errors are fixed.
Below is sample code and corresponding Valgrind output to give you an idea of the output vlagrind will throw on specific errors.
exampleCode used:
12 e T director()13 {14 e T * Arr = neu e T[10];fifteen e T * x = neu e T;sixteen e T * j;17 Arr[0] = *j; // and has not been initialized18 removed Arr; // Wrong removal, must be removed[]19 removed x;20 removed j; // Must not be removed, not on heap21 Returns 0;22 }
Valgrind output:
==22153== Using an uninitialized value of size 8==22153== at 0x4007B9: main (main.cpp:17)==22153====22153== Free() conflict /remove/remove[] = = 22153 == at 0x4C2B1CD: remove operator (void*) (vg_replace_malloc.c:576)== 22153== up to 0x4007E0: main (main.cpp:18)== 22153== address 0x5a22040 has 0 bytes in a block of size 40 alloc'd==22153== at 0x4C2A8E8: new operator[](unsigned long) (vg_replace_malloc.c:423)==22153== by 0x40079D: main (main.cpp:14)= =22153== = = 22153== Conditional jump or move depends on uninitialized value(s)==22153== at 0x40080F: main (main.cpp:20)==22153==22153= = Conditional jump or move depends on value (es) uninitialized ) from ). free() / delete / delete[] / realloc()==22153== at 0x4C2B1CD: operator delete(void*) (vg_replace_malloc.c: 576 )==22153== from 0x400820: main (main.cpp:20) ==22153== The address 0x400830 is in the text segment of /home/pacole 2/TA/examples/prog==22153== at 0x400830 : __libc_csu_init (at /home/pacole2/TA/examples/prog)==22153 ===22153====22153== HEAP SUMMARY:==22153 == in use on output: 0 bytes in 0 blocks = == 22153 == total heap usage: 2 allocations, 3 free, 44 bytes allocated == 22153 ==== 22153 == All stack blocks freed, no leaks possible==22153==22153 == For counts of detected and suppressed errors, rerun with: -v==22153== Use - -track-origins=y to see where uninitialized values come from==22153== BUG SUMMARY: 5 errors from 5 contexts (suppressed: 0 of 0)
The number 22153 represents the process ID (PID) that the operating system gave the program. Below is a breakdown of each error.
- The first error, coming from line 17, is because y was never initialized.
- The second error comes from the fact that line 18 uses the wrong delete. If you look at the declaration of
Arr
we can see itRemoved[]
should have been used instead. -
The last error occurs on line 20 when trying to remove something that has not been initialized or is pointing to the heap.
- Stack buffer overflow.This error occurs when limits are exceeded on an array created on the stack.
example
e T Arr[100];Arr[1] = 0;Arr[5 + 100]; //NOT
- use after return.This error occurs when you return a stack variable at the end of a function and try to use it after it is out of scope.
example
e T * arr1;Archive occupation() { e T arr2[100]; arr1 = arr2;}e T director() { occupation(); Returns arr1[10]; //NOT}
- Double charge or zero charge.Double free occurs when you release an item from the stack twice. Invalid deallocations occur when you deallocate a non-heap object.
example
e T * Arr = neu e T[100];removed [] Arr;removed [] Arr; //NOT
e T Arr[100];removed [] Arr; //NOT
additional exams
To test the code further, you can use the provided test program to see if your program works without memory leaks. We hope that your code does not cause memory leaks for both the store program and the test program. To compile the test program, run:
againTest
You can test whether the test program works without memory issues by running it
Valgrind ./test
If you don't see any errors or memory leak errors generated by Valgrind, then your test program also ran successfully. If not, it's time for Valgrind to work on the code again :)
For more specific issues related to memory leaks with Valgrind, you can find more information at:
door of your choice- Check for leaks=full exam
GDB: Ein Debugger
While Valgrind tells you what went wrong with your program after it runs, GDB is a debugging tool that lets you see what's going on in your program WHILE it's running. It can also be used for debugging purposes after program crashes. In particular, Valgrind works well for memory-related errors, but GDB can handle failures from these and other types of errors, such as logic errors. To learn how to use GDB for your lab and MPS, visit this page:
3 lines to find your segment error with GDB (use LLDB for macOS)
p.smemoria gdb# "memory" is the name of your executable(gdb)r# short for "run"(gdb)bt# Abbreviation for "return"
Guide: Learn GDB
Eligibility Information
The following files will be used to grade this assignment:
referrer.cpp
allocator.h
brief.cpp
letter.h
room.cpp
quarto.h
All other files incl.memoria.cpp
and all the test files you addedI will not do itused to qualify. Remember that shipments must be madeprairie jumper!