samedi 26 décembre 2020

does std::move make sense on a stack variable

I need to create a list of struct of size 1024. So I'm trying to optimize push operation into the list by making use of move semantics. But move semantics makes sense only for heap allocated memory(as per my knowledge). Can somebody suggest me a better way of doing this. This is my code

#include <iostream>
#include <list>
    
struct St {
    int32_t arr[1024];
};
    
int main() {
    std::list<St> struct_list;
    for(int32_t i = 0; i < 1024; ++i) {
        St st; // stack allocated and memory gets deallocated after each loop
        std::cout << &st << std::endl;
        struct_list.emplace_back(std::move(st)); // I feel std::move doesn't make any sense here even if I implement move constructor, still data gets copied into the std::list which is allocated in heap, so not efficient.
        std::cout << &struct_list.back() << "\n" << std::endl;
    }
        
    return EXIT_SUCCESS;
}

Aucun commentaire:

Enregistrer un commentaire