Commit ef1f6218acf191048e6af44b743f3ac68dcfea79
1 parent
0a04ac9d
fixed some memory stability issues
Showing
1 changed file
with
16 additions
and
14 deletions
Show diff stats
stim/math/matrix.h
... | ... | @@ -51,28 +51,24 @@ public: |
51 | 51 | matrix(size_t rows, size_t cols) { |
52 | 52 | R = rows; |
53 | 53 | C = cols; |
54 | - if (R * C == 0) | |
55 | - M = NULL; | |
56 | - else | |
57 | - M = new T[R * C]; | |
54 | + M = NULL; | |
55 | + if (R * C > 0) | |
56 | + M = (T*) malloc(R * C * sizeof(T)); | |
58 | 57 | } |
59 | 58 | |
60 | - matrix(size_t rows, size_t cols, T* data) { | |
59 | + matrix(size_t rows, size_t cols, const T* data) { | |
61 | 60 | R = rows; |
62 | 61 | C = cols; |
63 | - if (R * C == 0) | |
64 | - M = NULL; | |
65 | - else | |
66 | - M = new T[R * C]; | |
62 | + M = NULL; | |
63 | + if (R * C > 0) | |
64 | + M = (T*)malloc(R * C * sizeof(T)); | |
67 | 65 | memcpy(M, data, R * C * sizeof(T)); |
68 | 66 | } |
69 | 67 | |
70 | 68 | matrix(const matrix<T>& cpy){ |
71 | - | |
72 | - if (cpy.R * cpy.C == 0) | |
73 | - M = NULL; | |
74 | - else | |
75 | - M = new T[cpy.R * cpy.C]; | |
69 | + M = NULL; | |
70 | + if (cpy.R * cpy.C > 0) | |
71 | + M = (T*)malloc(cpy.R * cpy.C * sizeof(T)); | |
76 | 72 | memcpy(M, cpy.M, cpy.R * cpy.C * sizeof(T)); |
77 | 73 | |
78 | 74 | R = cpy.R; |
... | ... | @@ -222,6 +218,12 @@ public: |
222 | 218 | return result; |
223 | 219 | } |
224 | 220 | |
221 | + // Reshapes the matrix in place | |
222 | + void reshape(size_t rows, size_t cols) { | |
223 | + R = rows; | |
224 | + C = cols; | |
225 | + } | |
226 | + | |
225 | 227 | ///Calculate and return the determinant of the matrix |
226 | 228 | T det() const { |
227 | 229 | if (R != C) { | ... | ... |