Skip to content

Commit 6ba287f

Browse files
Merge pull request #2827 from AlexandreSinger/feature-dangling-reference
[NDMatrix] Resolved Dangling Reference Warning
2 parents c246f54 + a2be7c5 commit 6ba287f

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

libs/libvtrutil/src/vtr_ndmatrix.h

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ class NdMatrixProxy {
3030
* @brief Construct a matrix proxy object
3131
*
3232
* @param dim_sizes: Array of dimension sizes
33-
* @param idim: The dimension associated with this proxy
3433
* @param dim_stride: The stride of this dimension (i.e. how many element in memory between indicies of this dimension)
35-
* @param start: Pointer to the start of the sub-matrix this proxy represents
34+
* @param offset: The offset from the start that this sub-matrix starts at.
35+
* @param start: Pointer to the start of the base NDMatrix of this proxy
3636
*/
37-
NdMatrixProxy(const size_t* dim_sizes, const size_t* dim_strides, T* start)
37+
NdMatrixProxy(const size_t* dim_sizes, const size_t* dim_strides, size_t offset, const std::unique_ptr<T[]>& start)
3838
: dim_sizes_(dim_sizes)
3939
, dim_strides_(dim_strides)
40+
, offset_(offset)
4041
, start_(start) {}
4142

4243
NdMatrixProxy& operator=(const NdMatrixProxy& other) = delete;
@@ -50,7 +51,8 @@ class NdMatrixProxy {
5051
return NdMatrixProxy<T, N - 1>(
5152
dim_sizes_ + 1, // Pass the dimension information
5253
dim_strides_ + 1, // Pass the stride for the next dimension
53-
start_ + dim_strides_[0] * index); // Advance to index in this dimension
54+
offset_ + dim_strides_[0] * index, // Advance to index in this dimension
55+
start_); // Pass the base pointer.
5456
}
5557

5658
///@brief [] operator
@@ -60,9 +62,22 @@ class NdMatrixProxy {
6062
}
6163

6264
private:
65+
/// @brief The sizes of each dimension of this proxy. This is an array of
66+
/// length N.
6367
const size_t* dim_sizes_;
68+
69+
/// @brief The stride of each dimension of this proxy. This is an array of
70+
/// length N.
6471
const size_t* dim_strides_;
65-
T* start_;
72+
73+
/// @brief The offset from the base NDMatrix object that this sub-matrix
74+
/// starts at.
75+
size_t offset_;
76+
77+
/// @brief The pointer to the start of the base NDMatrix data. Since the
78+
/// base NDMatrix object owns the memory, we hold onto a reference
79+
/// to its unique pointer. This is safer than passing a bare pointer.
80+
const std::unique_ptr<T[]>& start_;
6681
};
6782

6883
///@brief Base case: 1-dimensional array
@@ -74,11 +89,13 @@ class NdMatrixProxy<T, 1> {
7489
*
7590
* @param dim_sizes: Array of dimension sizes
7691
* @param dim_stride: The stride of this dimension (i.e. how many element in memory between indicies of this dimension)
77-
* @param start: Pointer to the start of the sub-matrix this proxy represents
92+
* @param offset: The offset from the start that this sub-matrix starts at.
93+
* @param start: Pointer to the start of the base NDMatrix of this proxy
7894
*/
79-
NdMatrixProxy(const size_t* dim_sizes, const size_t* dim_stride, T* start)
95+
NdMatrixProxy(const size_t* dim_sizes, const size_t* dim_stride, size_t offset, const std::unique_ptr<T[]>& start)
8096
: dim_sizes_(dim_sizes)
8197
, dim_strides_(dim_stride)
98+
, offset_(offset)
8299
, start_(start) {}
83100

84101
NdMatrixProxy& operator=(const NdMatrixProxy& other) = delete;
@@ -89,7 +106,7 @@ class NdMatrixProxy<T, 1> {
89106
VTR_ASSERT_SAFE_MSG(index < dim_sizes_[0], "Index out of range (above dimension maximum)");
90107

91108
//Base case
92-
return start_[index];
109+
return start_[offset_ + index];
93110
}
94111

95112
///@brief [] operator
@@ -108,7 +125,7 @@ class NdMatrixProxy<T, 1> {
108125
* not to clobber elements in other dimensions
109126
*/
110127
const T* data() const {
111-
return start_;
128+
return start_.get() + offset_;
112129
}
113130

114131
///@brief same as above but allow update the value
@@ -118,9 +135,22 @@ class NdMatrixProxy<T, 1> {
118135
}
119136

120137
private:
138+
/// @brief The sizes of each dimension of this proxy. This is an array of
139+
/// length N.
121140
const size_t* dim_sizes_;
141+
142+
/// @brief The stride of each dimension of this proxy. This is an array of
143+
/// length N.
122144
const size_t* dim_strides_;
123-
T* start_;
145+
146+
/// @brief The offset from the base NDMatrix object that this sub-matrix
147+
/// starts at.
148+
size_t offset_;
149+
150+
/// @brief The pointer to the start of the base NDMatrix data. Since the
151+
/// base NDMatrix object owns the memory, we hold onto a reference
152+
/// to its unique pointer. This is safer than passing a bare pointer.
153+
const std::unique_ptr<T[]>& start_;
124154
};
125155

126156
/**
@@ -359,7 +389,8 @@ class NdMatrix : public NdMatrixBase<T, N> {
359389
return NdMatrixProxy<T, N - 1>(
360390
this->dim_sizes_.data() + 1, //Pass the dimension information
361391
this->dim_strides_.data() + 1, //Pass the stride for the next dimension
362-
this->data_.get() + this->dim_strides_[0] * index); //Advance to index in this dimension
392+
this->dim_strides_[0] * index, //Advance to index in this dimension
393+
this->data_); //Pass the base pointer
363394
}
364395

365396
/**

0 commit comments

Comments
 (0)