Skip to content

Commit 0236f4d

Browse files
Use std::vector instead of new to allocate arrays
1 parent b1f2870 commit 0236f4d

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

g2o/core/block_solver.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -276,28 +276,29 @@ bool BlockSolver<Traits>::buildStructure(bool zeroBlocks) {
276276
numLandmarks_ = 0;
277277
sizePoses_ = 0;
278278
sizeLandmarks_ = 0;
279-
int* blockPoseIndices = new int[optimizer_->indexMapping().size()];
280-
int* blockLandmarkIndices = new int[optimizer_->indexMapping().size()];
281279

282-
for (auto* v : optimizer_->indexMapping()) {
283-
int dim = v->dimension();
284-
if (!v->marginalized()) {
285-
v->setColInHessian(sizePoses_);
286-
sizePoses_ += dim;
287-
blockPoseIndices[numPoses_] = sizePoses_;
288-
++numPoses_;
289-
} else {
290-
v->setColInHessian(sizeLandmarks_);
291-
sizeLandmarks_ += dim;
292-
blockLandmarkIndices[numLandmarks_] = sizeLandmarks_;
293-
++numLandmarks_;
280+
{
281+
std::vector<int> blockPoseIndices(optimizer_->indexMapping().size());
282+
std::vector<int> blockLandmarkIndices(optimizer_->indexMapping().size());
283+
284+
for (auto* v : optimizer_->indexMapping()) {
285+
int dim = v->dimension();
286+
if (!v->marginalized()) {
287+
v->setColInHessian(sizePoses_);
288+
sizePoses_ += dim;
289+
blockPoseIndices[numPoses_] = sizePoses_;
290+
++numPoses_;
291+
} else {
292+
v->setColInHessian(sizeLandmarks_);
293+
sizeLandmarks_ += dim;
294+
blockLandmarkIndices[numLandmarks_] = sizeLandmarks_;
295+
++numLandmarks_;
296+
}
297+
sparseDim += dim;
294298
}
295-
sparseDim += dim;
299+
resize(blockPoseIndices.data(), numPoses_, blockLandmarkIndices.data(),
300+
numLandmarks_, sparseDim);
296301
}
297-
resize(blockPoseIndices, numPoses_, blockLandmarkIndices, numLandmarks_,
298-
sparseDim);
299-
delete[] blockLandmarkIndices;
300-
delete[] blockPoseIndices;
301302

302303
// allocate the diagonal on Hpp and Hll
303304
int poseIdx = 0;

g2o/solvers/csparse/csparse_wrapper.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,21 @@ class CSparse::Impl {
7474
cs_nfree(numericCholesky);
7575
numericCholesky = nullptr;
7676
}
77-
delete[] csWorkspace;
78-
csWorkspace = nullptr;
79-
delete[] csIntWorkspace;
80-
csIntWorkspace = nullptr;
8177
}
8278

8379
void prepareWorkspace() {
8480
// re-allocate the temporary workspace for cholesky
85-
if (csWorkspaceSize < ccsA.n) {
86-
csWorkspaceSize = csWorkspaceSize == 0 ? ccsA.n : 2 * ccsA.n;
87-
delete[] csWorkspace;
88-
csWorkspace = new double[csWorkspaceSize];
89-
delete[] csIntWorkspace;
90-
csIntWorkspace = new int[2L * csWorkspaceSize];
81+
if (csWorkspace.size() < ccsA.n) {
82+
const size_t desired_size = csWorkspace.empty() ? ccsA.n : 2 * ccsA.n;
83+
csWorkspace.resize(desired_size);
84+
csIntWorkspace.resize(2L * desired_size);
9185
}
9286
}
9387

9488
css* symbolicDecomposition = nullptr;
9589
int csWorkspaceSize = 0;
96-
double* csWorkspace = nullptr;
97-
int* csIntWorkspace = nullptr;
90+
std::vector<double> csWorkspace;
91+
std::vector<int> csIntWorkspace;
9892
csn* numericCholesky = nullptr;
9993
CSparseExt ccsA;
10094
};
@@ -150,8 +144,8 @@ bool CSparse::solve(double* x, double* b) const {
150144

151145
if (x != b) memcpy(x, b, pImpl_->ccsA.n * sizeof(double));
152146
const int ok = csparse_extension::cs_cholsolsymb(
153-
&pImpl_->ccsA, x, pImpl_->symbolicDecomposition, pImpl_->csWorkspace,
154-
pImpl_->csIntWorkspace);
147+
&pImpl_->ccsA, x, pImpl_->symbolicDecomposition,
148+
pImpl_->csWorkspace.data(), pImpl_->csIntWorkspace.data());
155149
return static_cast<bool>(ok);
156150
}
157151

@@ -193,8 +187,8 @@ bool CSparse::factorize() {
193187
pImpl_->prepareWorkspace();
194188
freeFactor();
195189
pImpl_->numericCholesky = csparse_extension::cs_chol_workspace(
196-
&pImpl_->ccsA, pImpl_->symbolicDecomposition, pImpl_->csIntWorkspace,
197-
pImpl_->csWorkspace);
190+
&pImpl_->ccsA, pImpl_->symbolicDecomposition,
191+
pImpl_->csIntWorkspace.data(), pImpl_->csWorkspace.data());
198192

199193
return pImpl_->numericCholesky != nullptr;
200194
}

0 commit comments

Comments
 (0)