Skip to content

Commit 569873f

Browse files
[NDMatrix] Resolved Dangling Reference Warning
GCC13 introduced a warning for potentially dangling references. The NDMatrix class was creating temporary objects to store the offseted pointer and then returning refrences to these pointers. Although this is correct, since the pointer is owned by the base NDMatrix object, it was confusing the compiler which is not something you want to do in general. Changed how the NDMatrix proxy class works by storing the offset into the pointer directly and passing a reference to the base pointer instead. This makes it clear to the compiler that this pointer belongs to another class and we are returning a reference to a portion of that memory, which is allowed.
1 parent c246f54 commit 569873f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

libs/libvtrutil/src/vtr_ndmatrix.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ class NdMatrixProxy {
3434
* @param dim_stride: The stride of this dimension (i.e. how many element in memory between indicies of this dimension)
3535
* @param start: Pointer to the start of the sub-matrix this proxy represents
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
@@ -62,7 +64,8 @@ class NdMatrixProxy {
6264
private:
6365
const size_t* dim_sizes_;
6466
const size_t* dim_strides_;
65-
T* start_;
67+
size_t offset_;
68+
const std::unique_ptr<T[]>& start_;
6669
};
6770

6871
///@brief Base case: 1-dimensional array
@@ -76,9 +79,10 @@ class NdMatrixProxy<T, 1> {
7679
* @param dim_stride: The stride of this dimension (i.e. how many element in memory between indicies of this dimension)
7780
* @param start: Pointer to the start of the sub-matrix this proxy represents
7881
*/
79-
NdMatrixProxy(const size_t* dim_sizes, const size_t* dim_stride, T* start)
82+
NdMatrixProxy(const size_t* dim_sizes, const size_t* dim_stride, size_t offset, const std::unique_ptr<T[]>& start)
8083
: dim_sizes_(dim_sizes)
8184
, dim_strides_(dim_stride)
85+
, offset_(offset)
8286
, start_(start) {}
8387

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

9195
//Base case
92-
return start_[index];
96+
return start_[offset_ + index];
9397
}
9498

9599
///@brief [] operator
@@ -108,7 +112,7 @@ class NdMatrixProxy<T, 1> {
108112
* not to clobber elements in other dimensions
109113
*/
110114
const T* data() const {
111-
return start_;
115+
return start_.get() + offset_;
112116
}
113117

114118
///@brief same as above but allow update the value
@@ -120,7 +124,8 @@ class NdMatrixProxy<T, 1> {
120124
private:
121125
const size_t* dim_sizes_;
122126
const size_t* dim_strides_;
123-
T* start_;
127+
size_t offset_;
128+
const std::unique_ptr<T[]>& start_;
124129
};
125130

126131
/**
@@ -359,7 +364,8 @@ class NdMatrix : public NdMatrixBase<T, N> {
359364
return NdMatrixProxy<T, N - 1>(
360365
this->dim_sizes_.data() + 1, //Pass the dimension information
361366
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
367+
this->dim_strides_[0] * index, //Advance to index in this dimension
368+
this->data_); //Pass the base pointer
363369
}
364370

365371
/**

0 commit comments

Comments
 (0)