Switch to using std::vector rather than directly managed memory

This commit is contained in:
John 2022-04-22 14:21:45 -05:00
parent b4a3beda69
commit aeeb33e699
4 changed files with 30 additions and 29 deletions

View File

@ -6,7 +6,7 @@ private:
int *resource_counts; int *resource_counts;
struct m{ struct m{
int x, y; int x, y;
int **data; std::vector<std::vector<int>> data;
} matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak? } matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak?
public: public:

View File

@ -1,42 +1,26 @@
#include <cstdlib> #include <cstdlib>
#include <vector>
#include "graph.hpp" #include "graph.hpp"
/* knotted:
Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
(!THIS MODIFIES THE GRAPH!)
@params: none, uses class-internal data
@returns:
int:
true if graph is knotted (deadlock)
false if graph is not (no deadlock)
*/
bool graph::knotted() {
return false;
}
graph::graph (const int processes, const int resources) { graph::graph (const int processes, const int resources) {
num_processes = processes; num_processes = processes;
num_resources = resources; num_resources = resources;
matrix.x = processes + resources; matrix.x = processes + resources;
matrix.y = matrix.x; matrix.y = matrix.x;
matrix.data = new int*[matrix.x]; // Reserve correct number of entries to minimize reallocation
matrix.data.reserve(matrix.x);
for (int x = 0; x < matrix.x; x++) { for (int x = 0; x < matrix.x; x++) {
matrix.data[x] = new int[matrix.y]; // Make a new x (vector<int>)
matrix.data.push_back({});
// Reserve correct number of entries
matrix.data[x].reserve(matrix.y);
for (int y = 0; y < matrix.y; y++) {
// Make a new y (int)
matrix.data[x].push_back({});
}
} }
} }
graph::~graph() { graph::~graph() {
/* clean up the 2D matrix */ /* clean up..? */
if (matrix.data) {
if (matrix.x || matrix.y) {
// delete y axes
for (int x = 0; x < matrix.x; x++) {
delete matrix.data[x];
}
// delete x axis
delete matrix.data;
matrix.x = 0; matrix.y = 0;
}
}
free(resource_counts);
} }

16
src/knotted.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <cstdlib>
#include <vector>
#include "graph.hpp"
/* knotted:
Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
(!THIS MODIFIES THE GRAPH!)
@params: none, uses class-internal data
@returns:
int:
true if graph is knotted (deadlock)
false if graph is not (no deadlock)
*/
bool graph::knotted() {
return false;
}

View File

@ -1,4 +1,5 @@
#include <cstdlib> #include <cstdlib>
#include <vector>
#include "graph.hpp" #include "graph.hpp"
/* reducible: /* reducible: