New user self-registration is disabled due to spam. Please email eigen-core-team @ if you need an account.
Before reporting a bug, please make sure that your Eigen version is up-to-date!
Bug 1049 - std::make_shared fails to fulfill structure aliment
Summary: std::make_shared fails to fulfill structure aliment
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: 3.2
Hardware: x86 - 32-bit All
: Lowest Feature Request
Assignee: Nobody
Depends on:
Blocks: 3.4
  Show dependency treegraph
Reported: 2015-07-29 10:15 UTC by Valentin Roussellet
Modified: 2019-02-18 20:31 UTC (History)
4 users (show)


Description Valentin Roussellet 2015-07-29 10:15:02 UTC
As described in the doc, structures having Eigen aligned members (e.g. Vector4) may be misaligned when created on the heap with new.

The solution for that issue is to overload the operator new, which is provided as a convenience macro to add in the said struct.

However std::make_shared ignores the overladed operator new as, in at least two implementations of the STD (GNU and MSCV) actually calls the global ::new.

These two codes, while semantically equivalent, behave differently.

std::shared_ptr<Foo> = std:::make_shared<Foo>(...)
std::shared_ptr<Foo> = std:::shared_ptr<Foo>(new Foo(...))

The second one yielding correct alignment while the first one may return a non-aligned pointer.

Would another macro specializing std::make_shared for all types requiring alignment be useful or practical ? 

If not, should Eigen provide a make_aligned_shared<T> implementation ?
Comment 1 Gael Guennebaud 2015-08-08 10:00:16 UTC
In C++11 there is std::allocate_shared that you can use with Eigen's aligned allocator.
Comment 2 Christos 2017-06-14 05:35:27 UTC
(In reply to Gael Guennebaud from comment #1)
> In C++11 there is std::allocate_shared that you can use with Eigen's aligned
> allocator.

std::allocate_shared fails as well in VS2017.
Not sure if it specific to msvc or not.

The code below is enough to reproduce it

class Foo
   Vec4d v;

auto test = std::allocate_shared<Foo>(Eigen::aligned_allocator<Foo>());
Comment 3 Gael Guennebaud 2019-02-18 20:31:52 UTC
Sorry for get back so late.

Works for me with gcc/clang (while make_shared does fail as expected).

If it don't work with MSVC, then this needs to be reported to VS team.

Note You need to log in before you can comment on or make changes to this bug.