Re: Assertion failure with multi-dimensional array and std::transform
tron.thomas@verizon.net wrote:
Debug Assertion Failed!
Program: ...
File: c:\program files\microsoft visual studio 8\vc\include\xutility
Line: 3348
Expression: ("_Current + _Off <= _Size && _Current + _Off >= 0", 0)
[...]
typedef float Matrix[4][4];
Matrix first =
{
{1.0f,1.0f,1.0f,1.0f},
{1.0f,1.0f,1.0f,1.0f},
{1.0f,1.0f,1.0f,1.0f},
{1.0f,1.0f,1.0f,1.0f}
};
Matrix second =
{
{1.0f,1.0f,1.0f,1.0f},
{1.0f,1.0f,1.0f,1.0f},
{1.0f,1.0f,1.0f,1.0f},
{1.0f,1.0f,1.0f,1.0f}
};
float* begin = first[0];
float* end = begin + (sizeof(first) / sizeof(*begin));
std::transform(begin, end, second[0], begin, std::plus<float>());
second[0] is a float[4], which std::transform detects and then refuses to
write to indices 4-15. IMHO, your program is flawed. It assumes that the
four arrays inside a Matrix are arranged like a single, flat array, but I
think the compiler is allowed to insert padding after each array.
I would suggest a different approach, using a class type instead of a mere
typedef. In fact I'd take a look at Boost's fixed-size array wrappers and
derive from those privately. You can then reuse things like begin()/end()
and iterators from the baseclass and add e.g. an operator[] that allows
access to the rows.
Uli