插入

时间:2018-07-15 04:33:15

标签: c++ dictionary memory allocation allocator

我正在编写自己的内存分配器,以跟踪应用程序中使用的内存。我目前仅在独立的测试床上进行测试。 到目前为止,我遇到的错误是C2664,这是我第一次使用std :: map遇到它。

这是地图:

std::map<intptr_t*, MemBlock> mAllocatedMemory;

这是尝试插入它的代码:

std::pair<std::_Tree<std::_Tmap_traits<void*, MemBlock, std::less<void*>, std::allocator<std::pair<void*const, MemBlock>>, false>>::iterator, bool> buffer = mAllocatedMemory.insert(std::make_pair(pmem->memAddress, pmem));

这是内存块结构:

typedef struct nMemBlock {
public:
    bool isMemBlockFree;
    size_t memSize;
    size_t memMemorySize;
    intptr_t * memAddress;
    nMemBlock * memNext;

public:
    nMemBlock(void) {}
    ~nMemBlock(void) {
        delete memAddress; memAddress = nullptr;
        delete memNext; memNext = nullptr;
    }
} MemBlock;

据我所言,这里是内存分配器,我正在测试以前从未写过的东西:

class MemAllocator {
    // Methods
public:
    MemBlock * AllocateMemBlock(size_t size) {
        MemBlock * block = (MemBlock*)malloc(0);
        void * memAddress = (void*)malloc(0);
        void * allocateMem = (void*)malloc(MemBlockSize * size);
        if (allocateMem == (void*)-1) {
            return nullptr;
        }
        else {
            block->isMemBlockFree = false;
            block->memAddress = static_cast<intptr_t*>(memAddress) + MemBlockSize;
            block->memMemorySize = sizeof(MemBlock) * size;
            block->memNext = nullptr;
            block->memSize = size;
        }
        return block;
    }

    // Constructors
public:
    MemAllocator() {

    }
    ~MemAllocator() {

    }

    // Operators
public:

    // Members
private:

};

这是我用于分配器的类:

class MemManager_Test : public MemAllocator
{
    // Methods
public:
    void AddMemoryBlock(size_t size) {
        MemBlock * pmem = AllocateMemBlock(size);

        if (pmem == nullptr) {
            // handle error
            return;
        }
    // this seems to be the line causing the problem, it's saying something about "no instance overloading"??
    std::pair<std::_Tree<std::_Tmap_traits<void*, MemBlock, std::less<void*>, std::allocator<std::pair<void*const, MemBlock>>, false>>::iterator, bool> buffer = mAllocatedMemory.insert(std::make_pair(pmem->memAddress, pmem));
}

    // Constructors
public:
    MemManager_Test() {

    }
    ~MemManager_Test() {

    }

    // Operators
public:

    // Members
private:
    std::map<intptr_t*, MemBlock> mAllocatedMemory;
    size_t mSize;
};

更新 似乎无法在构造时转换成对的第一个类型或intptr_t*

编译器输出说:

s-ct-main.cpp z:\游戏引擎\宁静引擎\代码\控制台测试平台\包含\ s-ct-mem-manager.h(163):错误C2664:'std :: _ Tree_iterator >> std :: _ Tree>: :insert(std :: _ Tree_const_iterator >>,const std :: pair&)':无法将参数1从'std :: pair'转换为'std :: pair &&'           与           [               _Ty = std :: pair <__ int64 * const,MemBlock>,               _Kty = intptr_t *,               _Pr = std :: less <__ int64 *>,               _Alloc = std :: allocator>           ]           和           [               _Kty = intptr_t *,               _Ty =记忆块           ]   z:\游戏引擎\宁静引擎\代码\控制台测试平台\ include \ s-ct-mem-manager.h(163):注意:原因:无法从'std :: pair'转换为'std ::对'           与           [               _Kty = intptr_t *,               _Ty =记忆块           ]   z:\游戏引擎\宁静引擎\代码\控制台测试平台\ include \ s-ct-mem-manager.h(163):注意:没有用户定义的转换运算符可以执行此转换,否则无法呼叫接线员

1 个答案:

答案 0 :(得分:1)

您要为其分配返回值auto res = mAllocatedMemory.insert(std::make_pair(pmem->memAddress, pmem)); 的对象类型不正确。

如果您可以使用C ++ 11或更高版本,请使用std::pair<std::map<intptr_t*, MemBlock>::iterator, bool>类型说明符。

std::pair<std::map<intptr_t*, MemBlock>::iterator, bool> res = 
   mAllocatedMemory.insert(std::make_pair(pmem->memAddress, pmem));

如果您使用的是该语言的早期版本,则需要使用显式类型std::map::insert

return PhysicalFile

有关public class HomeController : Controller { public IActionResult Download() { return PhysicalFile(@"d:\test\somemovie.mp4", "application/octet-stream"); } 返回类型的文档,请参见https://en.cppreference.com/w/cpp/container/map/insert