Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分配器在MSVC中是如何工作的?_C++_Visual C++_Memory Management - Fatal编程技术网

C++ 分配器在MSVC中是如何工作的?

C++ 分配器在MSVC中是如何工作的?,c++,visual-c++,memory-management,C++,Visual C++,Memory Management,我已经读过,他解释了分配器是如何工作的,但是当我实现下面的代码时,它总是给我0个结果。代码和MSVC有什么问题 分配器.h #pragma once #include <atomic> #include <memory> #include <iostream> #include <vector> #include <list> #include <set> namespace Milad { std::atom

我已经读过,他解释了分配器是如何工作的,但是当我实现下面的代码时,它总是给我0个结果。代码和MSVC有什么问题

分配器.h

#pragma once

#include <atomic>
#include <memory>
#include <iostream>
#include <vector>
#include <list>
#include <set>


namespace Milad
{
    std::atomic_int GMemoryUsed(0);

    template <typename T>
    class Allocator : public std::allocator<T>
    {
    private:
        using PrBase = std::allocator<T>;
        using PrPointer = typename std::allocator_traits<PrBase>::pointer;
        using PrSizeType = typename std::allocator_traits<PrBase>::size_type;
    public:
        Allocator() = default;

        template <typename U>
        Allocator(const Allocator<U>& arg_other) : PrBase(arg_other)
        {

        }

        template <typename U>
        struct rebind
        {
            using other = Allocator<U>;
        };

        PrPointer Allocate(PrSizeType arg_n)
        {
            GMemoryUsed.fetch_add(arg_n * sizeof(T));
            return PrBase::allocate(arg_n);
        }

        void Deallocate(PrPointer arg_p, PrSizeType arg_sz)
        {
            GMemoryUsed.fetch_sub(arg_sz * sizeof(T));
            PrBase::deallocate(arg_p, arg_sz);
        }
    };
}
#pragma一次
#包括
#包括
#包括
#包括
#包括
#包括
名称空间Mild
{
std::原子μint GMemoryUsed(0);
模板
类分配器:公共std::分配器
{
私人:
使用PrBase=std::分配器;
使用PrPointer=typename std::allocator\u traits::pointer;
使用PrSizeType=typename std::allocator\u traits::size\u type;
公众:
分配器()=默认值;
模板
分配器(常量分配器和参数其他):PrBase(参数其他)
{
}
模板
结构重新绑定
{
使用其他=分配器;
};
PrPointer Allocate(PrSizeType参数)
{
GMemoryUsed.fetch_add(arg_n*sizeof(T));
返回PrBase::分配(参数);
}
无效解除分配(PrPointer arg_p,PrSizeType arg_sz)
{
fetch_sub(arg_sz*sizeof(T));
PrBase::解除分配(arg_p,arg_sz);
}
};
}
Main.cpp

#include "Allocator.h"

template <template <typename T, typename AllocT> typename ContainerT>
void Measurement()
{
    std::cout << __FUNCSIG__ << std::endl;
    std::cout << "Before Memory Usage: " << Milad::GMemoryUsed.load() << std::endl;
    ContainerT<int, Milad::Allocator<int>> Container;
    for (int i = 0; i < 1000; ++i)
    {
        Container.insert(std::end(Container), i);
    }
    std::cout << "After Memory Usage: " << Milad::GMemoryUsed.load() << std::endl;
}

template <typename T, typename AllocT>
using SetWithDefaultComparator = std::set<T, std::less<>, AllocT>;

int main(int argc, const char* argv[])
{
    Measurement<std::vector>();
    Measurement<std::list>();
    Measurement<SetWithDefaultComparator>();

    return 0;
}
#包括“Allocator.h”
模板
空隙测量()
{

std::cout是的,您很接近。成员
allocate
deallocate
std::allocator
的成员,容器正在查找这些成员。由于您已将成员的版本大写,因此会调用默认分配器,而不会调用内存重写。您的代码应该是:

    PrPointer allocate(PrSizeType arg_n)
    {
        GMemoryUsed.fetch_add(arg_n * sizeof(T));
        return PrBase::allocate(arg_n);
    }

    void deallocate(PrPointer arg_p, PrSizeType arg_sz)
    {
        GMemoryUsed.fetch_sub(arg_sz * sizeof(T));
        PrBase::deallocate(arg_p, arg_sz);
    }

是的,您很接近。成员
allocate
deallocate
std::allocator
的成员,容器正在查找这些成员。由于您已将成员的版本大写,因此会调用默认分配器,而不会调用内存重写。您的代码应该是:

    PrPointer allocate(PrSizeType arg_n)
    {
        GMemoryUsed.fetch_add(arg_n * sizeof(T));
        return PrBase::allocate(arg_n);
    }

    void deallocate(PrPointer arg_p, PrSizeType arg_sz)
    {
        GMemoryUsed.fetch_sub(arg_sz * sizeof(T));
        PrBase::deallocate(arg_p, arg_sz);
    }
    PrPointer allocate(PrSizeType arg_n)
    {
        GMemoryUsed.fetch_add(arg_n * sizeof(T));
        return PrBase::allocate(arg_n);
    }

    void deallocate(PrPointer arg_p, PrSizeType arg_sz)
    {
        GMemoryUsed.fetch_sub(arg_sz * sizeof(T));
        PrBase::deallocate(arg_p, arg_sz);
    }