Skip to content

ArrayTuple: Initialization of larger structures #116

@xqms

Description

@xqms

Started from mosra/magnum#505. Here's a reproducing example using just Corrade:

#include <Corrade/Containers/ArrayTuple.h>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Utility/Debug.h>

using namespace Corrade;

int main(int argc, char** argv)
{
    Containers::ArrayView<std::pair<unsigned int, unsigned int>> arr;

    Containers::ArrayTuple tuple{
        {Containers::ValueInit, 4, arr}
    };

    for(std::size_t i = 0; i < arr.size(); ++i)
    {
        Utility::Debug{} << i;
        if(arr[i].second != 0)
            Utility::Debug{} << "Yes!";
    }

    return 0;
}

Valgrind gives warnings about uninitialized memory after half of the array. I believe the problem is this line:

/* If the item has a default constructor, call it on each element */
if(items[i]._constructor)
for(std::size_t j = 0; j != items[i]._elementCount; ++j)
items[i]._constructor(_data + offset + j*items[i]._elementAlignment);

where it should be items[i]._constructor(_data + offset + j*items[i]._elementSize); - at least that fixes the valgrind errors.

Probably a unit test with larger structures is required, that tests whether the memory has been correctly initialized.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions