moo*_*oid 0 c++ runtime-error c++20
我最近想用 C++ 制作一个快速模拟器,而不使用 C++(允许使用 C)标准库功能。因此,我使用原始指针数组来存储模拟器的内存。但是,我在将内存类移动到另一个内存类时遇到了读取访问冲突。欢迎所有建议。
内存等级:
#ifndef MEM_HPP
#define MEM_HPP
#include <cstdint>
#include <cstddef>
namespace handle {
using num = std::uint8_t;
using size = std::size_t;
struct memory {
enum { EIGHT_BIT_MAX_MEM = 256, SIXTEEN_BIT_MAX_MEM = 65536 };
constexpr explicit memory(size max_mem) : mem_size(max_mem) {
mem = new num[max_mem];
}
constexpr memory(memory&& other) noexcept {
delete[] mem;
mem = other.mem;
mem_size = other.mem_size;
other.mem = nullptr;
other.mem_size = 0;
}
constexpr memory(const memory& other) = delete;
constexpr ~memory() {
delete[] mem;
}
constexpr memory& operator=(memory&& other) noexcept {
delete[] mem;
mem = other.mem;
mem_size = other.mem_size;
other.mem = nullptr;
other.mem_size = 0;
return *this;
}
constexpr num& operator[](num loc) {
return mem[loc];
}
constexpr size get_mem_size() const noexcept {
return mem_size;
}
num *mem;
size mem_size;
};
}
#endif /* MEM_HPP */
Run Code Online (Sandbox Code Playgroud)
主要.cpp:
#include <type_traits>
#include "mem.hpp"
int main() {
using namespace handle;
memory m{ memory::EIGHT_BIT_MAX_MEM };
memory other{ std::move(m) };
}
Run Code Online (Sandbox Code Playgroud)
编辑:即使我删除将内存初始化为空指针的构造函数,我仍然会遇到读访问冲突。
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |