4

What happens

When I try to add an Eigen::Vector3f into an std::vector following the tutorial on Eigen website like this:

#include <Eigen/Core>
#include <Eigen/StdVector>
#include <iostream>

template <class EigenT>
using EigenStdVector = std::vector<EigenT, Eigen::aligned_allocator<EigenT>>;

int main() {
  EigenStdVector<Eigen::Vector3f> vec;
  vec.emplace_back(1.0f, 1.0f, 1.0f);
  std::cerr << vec.back().transpose() << std::endl;
  return 0;
}

I get the following warning:

In file included from /usr/include/eigen3/Eigen/Core:349:0,
                 from /home/igor/Code/eigen_example/example.cpp:3:
In function ‘void* Eigen::internal::aligned_malloc(std::size_t)’,
    inlined from ‘void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {float, float, float}; _Tp = Eigen::Matrix<float, 3, 1>; _Alloc = Eigen::aligned_allocator<Eigen::Matrix<float, 3, 1> >]’ at /usr/include/eigen3/Eigen/src/Core/util/Memory.h:742:76:
/usr/include/eigen3/Eigen/src/Core/util/Memory.h:159:12: warning: argument 1 value ‘18446744073709551612’ exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
     result = std::malloc(size);

How to reproduce

I am on Ubuntu 18.04 and have Eigen 3.3.4 installed.

You can build the code in this question with the following command if you have Eigen installed with the following command:

g++ -I/usr/include/eigen3 -O3 example.cpp

What triggers the warning

  • The warning only shows when using Vector2f, Vector3f, etc. It is not shown for types like Matrix2f, Matrix3f, etc.
  • The warning is shown whenever I have -O[1-3] enabled and does not happen with -O0

Further info:

This question seems to be related, but I don't see how it could help me.

I have made a small example for everyone who wants a ready-to-run example. You can find it on my GitHub.

Does anybody have an idea what might be wrong here?

niosus
  • 738
  • 8
  • 22
  • 1
    That value is 2^64-4, which is surely relevant. – Davis Herring Dec 26 '18 at 18:52
  • 2
    What version of Eigen are you using? (Check in `/usr/include/eigen3/Eigen/src/Core/util/Macros.h`) And can you give a one-line compiler command, instead of the CMakelist? (build with `VERBOSE=1`) – chtz Dec 27 '18 at 00:02
  • @chtz I have updated the question. Just for simplicity will double relevant info here. Version of Eigen: 3.3.4, a command to build the code to reproduce this: `g++ -I/usr/include/eigen3 -O3 example.cpp` – niosus Dec 27 '18 at 14:53

1 Answers1

3

In the file Eigen/src/Core/util/Memory.h in the implementation of Eigen::aligned_allocator these lines can be found:

#if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_AT_LEAST(7,0)
// In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
// eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
size_type max_size() const {
  return (std::numeric_limits<std::ptrdiff_t>::max)()/sizeof(T);
}
#endif

So, it seems that this is connected to this GCC bug.

As far as I can see, the commit fixing this appeared on 2018-10-07, and should be available in Eigen 3.3.6.

lisyarus
  • 15,025
  • 3
  • 43
  • 68