This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Weird std::bad_alloc

Tags: None
(comma "," separated)
Horus
Registered Member
Posts
296
Karma
0
OS

Weird std::bad_alloc

Mon Feb 09, 2015 9:33 am
Eigen 3.2.4, GCC 4.9.2 20141224, Linux 3.18.5 / Arch, 64bit

Hello,

I have a piece of code:

Code: Select all
#include <Eigen/Core>
#include <Eigen/Dense>

int main(int argc, char **args)
{
  const int n = 500;
  const int m = 20;
  auto J = Eigen::MatrixXd(n, n);
  auto V = Eigen::MatrixXd(n, m);

  // fill with some values
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      J(j, i) = j*i;
      V(j, i) = j*i * 0.5;
    }
  }

  auto Z = V.householderQr().solve(Eigen::MatrixXd::Identity(n, n));
  auto Jn = (J * V) * Z;
  return 0;
}


and compile it with:

Code: Select all
g++ -std=c++11 -L eigen -I eigen -Wall $FLAGS eigen.cpp giving no messages 


If FLAGS = -O0 executing works without any error.
If FLAGS = -O1 or -O2 or -O3 it crashes with:

Code: Select all
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
[1]    25106 abort (core dumped)  ./a.out


Compilation with FLAGS = -02 -g3 gives an backtrace within gdb:

Code: Select all
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Program received signal SIGABRT, Aborted.
0x00007ffff7241a97 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7241a97 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7242e6a in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7b2cfcd in __gnu_cxx::__verbose_terminate_handler() () from 
/usr/lib/libstdc++.so.6
#3  0x00007ffff7b2ae56 in __cxxabiv1::__terminate(void (*)()) () from 
/usr/lib/libstdc++.so.6
#4  0x00007ffff7b2aea1 in std::terminate() () from /usr/lib/libstdc++.so.6
#5  0x00007ffff7b2b0b8 in __cxa_throw () from /usr/lib/libstdc++.so.6
#6  0x0000000000401137 in Eigen::internal::throw_std_bad_alloc () at 
eigen/Eigen/src/Core/util/Memory.h:88
#7  0x0000000000401604 in check_size_for_overflow<double> (size=<optimized 
out>) at eigen/Eigen/src/Core/util/Memory.h:366
#8  conditional_aligned_new_auto<double, true> (size=<optimized out>) at 
eigen/Eigen/src/Core/util/Memory.h:422
#9  resize (nbCols=<optimized out>, nbRows=<optimized out>, size=<optimized 
out>, this=<optimized out>) at eigen/Eigen/src/Core/DenseStorage.h:253
#10 Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> 
>::resize (this=0x7fffffffdd18, nbRows=6469792, nbCols=500) at 
eigen/Eigen/src/Core/PlainObjectBase.h:250
#11 0x000000000040882c in 
Matrix<Eigen::internal::solve_retval_base<Eigen::HouseholderQR<Eigen::Matrix<double, 
-1, -1> >, 
Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, 
Eigen::Matrix<double, -1, -1> > > > (
    other=..., this=0x7fffffffdd18) at eigen/Eigen/src/Core/Matrix.h:295
#12 
Eigen::ProductBase<Eigen::GeneralProduct<Eigen::GeneralProduct<Eigen::Matrix<double, 
-1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, 5>, 
Eigen::ReturnByValue<Eigen::internal::solve_retval_base<Eigen::HouseholderQR<Eigen::Matrix<double, 
-1, -1, 0, -1, -1> >, 
Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, 
Eigen::Matrix<double, -1, -1, 0, -1, -1> > > >, 5>, 
Eigen::GeneralProduct<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 
Eigen::Matrix<double, -1, -1, 0, -1, -1>, 5>, 
Eigen::ReturnByValue<Eigen::internal::solve_retval_base<Eigen::HouseholderQR<Eigen::Matrix<double, 
-1, -1, 0, -1, -1> >, 
Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, 
Eigen::Matrix<double, -1, -1, 0, -1, -1> > > > >::ProductBase 
(this=this@entry=0x7fffffffdd10, a_lhs=..., a_rhs=...) at 
eigen/Eigen/src/Core/ProductBase.h:98
#13 0x0000000000400b4d in GeneralProduct (rhs=..., lhs=..., 
this=0x7fffffffdd10) at 
eigen/Eigen/src/Core/products/GeneralMatrixMatrix.h:391
#14 
operator*<Eigen::ReturnByValue<Eigen::internal::solve_retval_base<Eigen::HouseholderQR<Eigen::Matrix<double, 
-1, -1> >, 
Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, 
Eigen::Matrix<double, -1, -1> > > > > (
    other=..., this=0x7fffffffdd50) at 
eigen/Eigen/src/Core/GeneralProduct.h:595
#15 main (argc=<optimized out>, args=<optimized out>) at eigen.cpp:26


Any idea what's wrong here?

Thanks,
Florian
jitseniesen
Registered Member
Posts
204
Karma
2

Re: Weird std::bad_alloc

Tue Feb 10, 2015 8:55 pm
Does it work if you replace all the "auto" declarations with the correct type (I think "MatrixXd" in all the cases)? Eigen is an expression template library and most functions return an expression object which is only evaluated when it is assigned to a variable of type Matrix. As a consequence, it is dangerous and usually wrong to use "auto".
Horus
Registered Member
Posts
296
Karma
0
OS

Re: Weird std::bad_alloc

Wed Feb 11, 2015 8:11 am
Oh yes, that did the trick. It works now. Thanks!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]