aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Christoph Hertzberg <chtz@informatik.uni-bremen.de>2014-09-25 16:05:17 +0200
committerGravatar Christoph Hertzberg <chtz@informatik.uni-bremen.de>2014-09-25 16:05:17 +0200
commit4ba8aa14822ae0b444baa9e0d41d8744cdcd6b03 (patch)
treec1c0abd45012c09a8e2cf9583226b146d41ce501
parent27d6b4daf90488e105d70f45e5d5684b8f1a906d (diff)
Fix bug #884: No malloc for zero-sized matrices or for Ref without temporaries
-rw-r--r--Eigen/src/Core/util/Memory.h2
-rw-r--r--test/nomalloc.cpp35
2 files changed, 36 insertions, 1 deletions
diff --git a/Eigen/src/Core/util/Memory.h b/Eigen/src/Core/util/Memory.h
index 30133ba67..af46c449c 100644
--- a/Eigen/src/Core/util/Memory.h
+++ b/Eigen/src/Core/util/Memory.h
@@ -454,6 +454,8 @@ template<typename T, bool Align> inline T* conditional_aligned_realloc_new(T* pt
template<typename T, bool Align> inline T* conditional_aligned_new_auto(size_t size)
{
+ if(size==0)
+ return 0; // short-cut. Also fixes Bug 884
check_size_for_overflow<T>(size);
T *result = reinterpret_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T)*size));
if(NumTraits<T>::RequireInitialization)
diff --git a/test/nomalloc.cpp b/test/nomalloc.cpp
index cbd02dd21..5458806f5 100644
--- a/test/nomalloc.cpp
+++ b/test/nomalloc.cpp
@@ -165,6 +165,38 @@ void ctms_decompositions()
Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV);
}
+void test_zerosized() {
+ // default constructors:
+ Eigen::MatrixXd A;
+ Eigen::VectorXd v;
+ // explicit zero-sized:
+ Eigen::ArrayXXd A0(0,0);
+ Eigen::ArrayXd v0(0);
+
+ // assigning empty objects to each other:
+ A=A0;
+ v=v0;
+}
+
+template<typename MatrixType> void test_reference(const MatrixType& m) {
+ typedef typename MatrixType::Scalar Scalar;
+ enum { Flag = MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor};
+ enum { TransposeFlag = !MatrixType::IsRowMajor ? Eigen::RowMajor : Eigen::ColMajor};
+ typename MatrixType::Index rows = m.rows(), cols=m.cols();
+ // Dynamic reference:
+ typedef Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Flag > > Ref;
+ typedef Eigen::Ref<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, TransposeFlag> > RefT;
+
+ Ref r1(m);
+ Ref r2(m.block(rows/3, cols/4, rows/2, cols/2));
+ RefT r3(m.transpose());
+ RefT r4(m.topLeftCorner(rows/2, cols/2).transpose());
+
+ VERIFY_RAISES_ASSERT(RefT r5(m));
+ VERIFY_RAISES_ASSERT(Ref r6(m.transpose()));
+ VERIFY_RAISES_ASSERT(Ref r7(Scalar(2) * m));
+}
+
void test_nomalloc()
{
// check that our operator new is indeed called:
@@ -175,5 +207,6 @@ void test_nomalloc()
// Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms)
CALL_SUBTEST_4(ctms_decompositions<float>());
-
+ CALL_SUBTEST_5(test_zerosized());
+ CALL_SUBTEST_6(test_reference(Matrix<float,32,32>()));
}