Memset - array of a pair in C ++

I have an array of pairs. I want to use memset on this array to do 0, 1 or -1. How should I do it? Is this the usual procedure, as always?

Pair and array:

std::pair<int, int> ar[100][100];

      

+3


source to share


3 answers


Don't use memset

for anything that isn't a POD (which is std::pair

n't) - that won't be good (it's undefined behavior).

Classes with constructors need constructors to be called - they memset

don't. Classes with members POD

need constructors for those elements called - memset

, they don't.



For small arrays, instead of aggregate initialization . For large arrays, you can use a loop. And there is also std :: fill .

I would also recommend using std :: array on a C-style array when the size is known at compile time, or std :: vector otherwise.

+4


source


As mentioned, steam is not a POD.

However, the data can be restructured as a POD with little loss in usefulness:

#include <tuple>
#include <utility>
#include <cstring>

/* this is a POD */
using pair_matrix = int [100][100][2];

extern std::tuple<int, int> foo();
extern void bar(std::tuple<int&, int&>);

int main()
{
    pair_matrix m;

    /* we can memset PODS */
    std::memset(std::addressof(m), sizeof(m), 0);

    /* we can convert the 2-d array into a tuple easily */
    auto at = [&m](int x, int y) {
        auto& a = m[y][x];
        return std::tie(a[0], a[1]);
    };

    /* and manipulate as a tuple */
    at(10,10) = foo();
    bar(at(10,10));
}

      



Another (possibly more reliable) way is to reinvent the concept of a multidimensional array, which has a method fill

:

#include <tuple>
#include <utility>
#include <memory>

template<class Type, std::size_t...Dimensions> struct dimension_view;
template<class Type> struct dimension_view<Type>
{
    static constexpr std::size_t extent = 1;
    static constexpr std::size_t size() { return extent; }
    static constexpr std::size_t storage_size = 1;

    using storage_type = Type;

    constexpr dimension_view(Type* data) : data_(data) {}

    operator Type&() { return *data_; }

    template<class Other>
    constexpr Type& operator=(Other&& other) {
        *data_ = std::forward<Other>(other);
        return *data_;
    }

    storage_type* data_;
};

template<class Type, std::size_t Dim, std::size_t...Rest> 
struct dimension_view<Type, Dim, Rest...>
{
    using next = dimension_view<Type, Rest...>;
    static constexpr std::size_t extent = Dim;
    static constexpr std::size_t size() { return extent; }
    static constexpr std::size_t storage_size = Dim * next::storage_size;

    using storage_type = Type [storage_size];

    constexpr dimension_view(Type* data) : data_(data) {}

    auto begin() -> Type*
    {
        return data_;
    }
    auto end() -> Type*
    {
        return std::addressof(data_[extent * next::storage_size]);
    }

    auto operator[](std::size_t i) {
        return next(std::addressof(data_[i * next::storage_size]));
    }

    Type* data_ ;
};

template<class T, std::size_t...Dims>
struct multi_dimensional_array
{
    using view_type = dimension_view<T, Dims...>;
    using storage_type = typename view_type::storage_type;

    template<class...Args, std::enable_if_t<std::is_nothrow_constructible<T, Args...>::value>* = nullptr>
    constexpr multi_dimensional_array(Args&&...args)
    {
        for(auto&& store : data_)
        {
            new (std::addressof(store)) T (args...); 
        }
    }

    template<class...Args, std::enable_if_t<not std::is_nothrow_constructible<T, Args...>::value>* = nullptr>
    multi_dimensional_array(Args&&...args)
    {
        auto count = std::size_t(0);
        try {
            for(auto&& store : data_)
            {
                new (std::addressof(store)) T (args...); 
                ++count;
            }
        }
        catch(...) {
            destroy(count);
        }
    }

    /* delete copies for now */
    multi_dimensional_array(multi_dimensional_array const&) = delete;
    multi_dimensional_array& operator=(multi_dimensional_array const&) = delete;
    multi_dimensional_array(multi_dimensional_array &&) = delete;
    multi_dimensional_array& operator=(multi_dimensional_array &&) = delete;

    ~multi_dimensional_array()
    {
        destroy(view_type::storage_size);
    }

    void destroy(std::size_t last)
    {
        while(last--) {
            reinterpret_cast<T*>(get_data()+last)->~T();        }
    }

    constexpr auto begin_storage() { 
        return view_type(get_data()).begin();
    }

    constexpr auto end_storage() { 
        return view_type(get_data()).end();
    }

    constexpr auto fill(T value) -> void
    {
        std::fill(begin_storage(), end_storage(), value);
    }

    constexpr auto operator[](std::size_t i) -> decltype(auto) {
        return view_type(get_data())[i];
    }

    constexpr T* get_data() {
        return reinterpret_cast<T*>(data_);
    }

    std::aligned_storage_t<sizeof(T), alignof(T)> data_[view_type::storage_size];
};

using pair_matrix = multi_dimensional_array<std::pair<int, int>, 100, 100>;


void force_view(pair_matrix&);
void force_deref(std::pair<int, int>&);
std::size_t getval();

int main()
{
    /* create with initial values */
    pair_matrix m(std::make_pair(-1, 1));

    /* fill the entire multidimentional array */
    m.fill(std::make_pair(-1, -1));

    m[10][10] = std::make_pair(1,1);

    /* force compiler to generate some code to exercise implementation */
    force_view(m);
    force_deref(m[getval()][getval()]);
}

      

+1


source


The C ++ way is to use, std::fill

instead of memset

, something like:

std::pair<int, int> ar[100][100];
std::fill(&ar[0][0], &ar[0][0] + 100 * 100, std::make_pair(-1, -1));

      

+1


source







All Articles