From 8cc3c9eed77a282533a225b9787b4d18c16a399e Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Wed, 22 Feb 2017 18:27:40 +0100 Subject: [PATCH] Fix AlignedVector::push_back for types without default constructor. --- .../changes/minor/20170222MartinKronbichler_1 | 4 ++ include/deal.II/base/aligned_vector.h | 5 +- tests/base/aligned_vector_05.cc | 69 +++++++++++++++++++ tests/base/aligned_vector_05.output | 4 ++ tests/matrix_free/copy_feevaluation.cc | 4 +- 5 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 doc/news/changes/minor/20170222MartinKronbichler_1 create mode 100644 tests/base/aligned_vector_05.cc create mode 100644 tests/base/aligned_vector_05.output diff --git a/doc/news/changes/minor/20170222MartinKronbichler_1 b/doc/news/changes/minor/20170222MartinKronbichler_1 new file mode 100644 index 0000000000..c5f9d298e6 --- /dev/null +++ b/doc/news/changes/minor/20170222MartinKronbichler_1 @@ -0,0 +1,4 @@ +Fixed: The function AlignedVector::push_back did not work correctly for +types that do not provide a default constructor. This is now fixed. +
+(Martin Kronbichler, 2017/02/22) diff --git a/include/deal.II/base/aligned_vector.h b/include/deal.II/base/aligned_vector.h index 6961b437d8..e852143cd9 100644 --- a/include/deal.II/base/aligned_vector.h +++ b/include/deal.II/base/aligned_vector.h @@ -699,8 +699,9 @@ AlignedVector::push_back (const T in_data) if (_end_data == _end_allocated) reserve (std::max(2*capacity(),static_cast(16))); if (std_cxx11::is_trivial::value == false) - new (_end_data) T; - *_end_data++ = in_data; + new (_end_data++) T(in_data); + else + *_end_data++ = in_data; } diff --git a/tests/base/aligned_vector_05.cc b/tests/base/aligned_vector_05.cc new file mode 100644 index 0000000000..64f6129d21 --- /dev/null +++ b/tests/base/aligned_vector_05.cc @@ -0,0 +1,69 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2012 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// test for AlignedVector::push_back + +#include "../tests.h" +#include +#include +#include + +#include + +// dummy class without default constructor +class foo +{ +public: + foo (const unsigned int a) : vec(1, a) {} + foo (const foo &bar) : vec(bar.vec) {} + foo &operator= (const foo &bar) + { + vec = bar.vec; + return *this; + } + + unsigned int element() const + { + return vec[0]; + } + +private: + AlignedVector vec; +}; + +void test () +{ + AlignedVector vec; + vec.push_back(foo(3)); + vec.resize(2, foo(6)); + deallog << vec[0].element() << " " << vec[1].element() << std::endl; + vec[1] = foo(13); + deallog << vec[0].element() << " " << vec[1].element() << std::endl; + vec.reserve(3); + deallog << vec[0].element() << " " << vec[1].element() << std::endl; +} + + + + +int main() +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + test (); +} diff --git a/tests/base/aligned_vector_05.output b/tests/base/aligned_vector_05.output new file mode 100644 index 0000000000..62c574ce92 --- /dev/null +++ b/tests/base/aligned_vector_05.output @@ -0,0 +1,4 @@ + +DEAL::3 6 +DEAL::3 13 +DEAL::3 13 diff --git a/tests/matrix_free/copy_feevaluation.cc b/tests/matrix_free/copy_feevaluation.cc index 6f607c2cdf..2e07122a80 100644 --- a/tests/matrix_free/copy_feevaluation.cc +++ b/tests/matrix_free/copy_feevaluation.cc @@ -71,8 +71,8 @@ public: { typedef VectorizedArray vector_t; // allocate FEEvaluation. This test will test proper alignment - AlignedVector > velocity - (1, FEEvaluation(data, 0)); + AlignedVector > velocity; + velocity.push_back(FEEvaluation(data, 0)); AlignedVector > pressure (1, FEEvaluation(data, 1)); FEEvaluation pressure2(data, 1); -- 2.39.5