小编Fur*_*ran的帖子

为什么在自定义 STL、C++ VS2019 中创建 2 个变量会导致崩溃?

你好,我正在尝试重写我自己的内存管理器和 STL(没什么特别的,只是一些基本的向量和字符串功能),但我得到了一个奇怪的行为。我正在尝试获得内存管理领域的经验,因为我是一名有空闲时间的高中生。问题是,当我创建第一个变量时一切正常,但是在创建第二个变量后,程序在创建第一个变量时崩溃。

字符串.h/.cpp

    class String {
        char* pointer_toBuffer = nullptr;
        size_t buffer_length = 0;
        IAllocator* Allocator;
    public:
        String(const char* text, IAllocator* Allocator);}

    String::String(const char* text, TuranAPI::MemoryManagement::IAllocator* MemoryAllocator) : Allocator(MemoryAllocator) {
        std::cout << "String creation has started: " << text << std::endl;
        unsigned int i = 0;
        while (text[i] != 0) {
            i++;
        }
        buffer_length = i + 1;
        pointer_toBuffer = (char*)Allocator->Allocate_MemoryBlock(buffer_length * sizeof(char));//When I write the Second String part, FirstString crashes directly. I use VSDebug and it says access violation …
Run Code Online (Sandbox Code Playgroud)

c++ string memory-management visual-studio-2019

6
推荐指数
1
解决办法
111
查看次数