]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix AlignedVector::push_back for types without default constructor. 3993/head
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Wed, 22 Feb 2017 17:27:40 +0000 (18:27 +0100)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Wed, 22 Feb 2017 17:45:42 +0000 (18:45 +0100)
doc/news/changes/minor/20170222MartinKronbichler_1 [new file with mode: 0644]
include/deal.II/base/aligned_vector.h
tests/base/aligned_vector_05.cc [new file with mode: 0644]
tests/base/aligned_vector_05.output [new file with mode: 0644]
tests/matrix_free/copy_feevaluation.cc

diff --git a/doc/news/changes/minor/20170222MartinKronbichler_1 b/doc/news/changes/minor/20170222MartinKronbichler_1
new file mode 100644 (file)
index 0000000..c5f9d29
--- /dev/null
@@ -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.
+<br>
+(Martin Kronbichler, 2017/02/22)
index 6961b437d8637ecfa7a493a46190a922671f5f84..e852143cd9b2493d065ec5b79785553421a6cb2a 100644 (file)
@@ -699,8 +699,9 @@ AlignedVector<T>::push_back (const T in_data)
   if (_end_data == _end_allocated)
     reserve (std::max(2*capacity(),static_cast<size_type>(16)));
   if (std_cxx11::is_trivial<T>::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 (file)
index 0000000..64f6129
--- /dev/null
@@ -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 <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/aligned_vector.h>
+
+// 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<unsigned int> vec;
+};
+
+void test ()
+{
+  AlignedVector<foo> 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 (file)
index 0000000..62c574c
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::3 6
+DEAL::3 13
+DEAL::3 13
index 6f607c2cdfde84423105060149d94ba8f4a1c448..2e07122a80eadce4cc43e0d49ad234e454132b72 100644 (file)
@@ -71,8 +71,8 @@ public:
   {
     typedef VectorizedArray<Number> vector_t;
     // allocate FEEvaluation. This test will test proper alignment
-    AlignedVector<FEEvaluation<dim,degree_p+1,degree_p+2,dim,Number> > velocity
-    (1, FEEvaluation<dim,degree_p+1,degree_p+2,dim,Number>(data, 0));
+    AlignedVector<FEEvaluation<dim,degree_p+1,degree_p+2,dim,Number> > velocity;
+    velocity.push_back(FEEvaluation<dim,degree_p+1,degree_p+2,dim,Number>(data, 0));
     AlignedVector<FEEvaluation<dim,degree_p,  degree_p+2,1,  Number> > pressure
     (1, FEEvaluation<dim,degree_p,  degree_p+2,1,  Number>(data, 1));
     FEEvaluation<dim,degree_p,  degree_p+2,1,  Number> pressure2(data, 1);

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.