制作指向已分配内存的 C++ 向量

Making a C++ vector that points to already allocated memory

我正在使用一些较旧的代码,这些代码 malloc 一块 RAM,然后将二进制文件加载到其中。二进制文件是一系列 8 位灰度图像平面 X x Y 大小,Z 平面深度。这些文件通常在 500 兆字节到 10 GB 之间。

现有代码使用复杂的指针排列来访问 XY、XZ 或 YZ 平面中的各个平面。

我想做的是将指针替换为单个向量向量,其中每个子向量是数据中的一个 XY 平面。这样做的目的是获得一些安全性并检查您使用向量而不使用原始指针访问。

基于上一个问题 (Is it possible to initialize std::vector over already allocated memory?) 我有以下代码

//preallocate.h
template <typename T>
class PreAllocator
{
private:
T* memory_ptr;
std::size_t memory_size;

public:
typedef std::size_t     size_type;
typedef T*              pointer;
typedef T               value_type;

PreAllocator(T* memory_ptr, std::size_t memory_size) : memory_ptr(memory_ptr), memory_size(memory_size) {}

PreAllocator(const PreAllocator& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};

template<typename U>
PreAllocator(const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};

template<typename U>
PreAllocator& operator = (const PreAllocator<U>& other) { return *this; }
PreAllocator<T>& operator = (const PreAllocator& other) { return *this; }
~PreAllocator() {}


pointer allocate(size_type n, const void* hint = 0) {return memory_ptr;}
void deallocate(T* ptr, size_type n) {}

size_type max_size() const {return memory_size;}
};

简化后的主函数如下所示:

TOMhead header;
uint8_t* TOMvolume;
size_t volumeBytes = 0;

int main(int argc, char *argv[])
{
    std::fstream TOMfile;
    std::ios_base::iostate exceptionMask = TOMfile.exceptions() | std::ios::failbit| std::ifstream::badbit;
    TOMfile.exceptions(exceptionMask);

    try {
        TOMfile.open(argv[1],std::ios::in|std::ios::binary);
    }
    catch (std::system_error& error) {
        std::cerr << error.code().message() << std::endl;
        return ERROR;
    }

    TOMfile.read((char*) &header, sizeof(header));
    if (!TOMfile)
    {
        std::cout<<"Error reading file into memory, expected to read " << sizeof(header) << " but only read " << TOMfile.gcount() << "bytes" <<std::endl;
        return ERROR;
    }


    TOMfile.seekg(std::ios_base::beg);      // rewind to begining of the file
    TOMfile.seekg(sizeof(header));          // seek to data beyond the header

    volumeBytes = (header.xsize * header.ysize * header.zsize);

    std::cout << "Trying to malloc " << volumeBytes << " bytes of RAM" << std::endl;

    TOMvolume = (uint8_t*) malloc(volumeBytes);
    if (TOMvolume == NULL)
    {
        std::cout << "Error allocating RAM for the data" << std::endl;
        return ERROR;
    }
TOMfile.read((char*) TOMvolume,volumeBytes);

然后我尝试使用预分配器创建一个向量来保存这个 malloc 数据

std::vector<uint8_t, PreAllocator<uint8_t>> v_TOMvolume(0, PreAllocator<uint8_t>(&TOMvolume[0], volumeBytes));
v_TOMvolume.push_back(volumeBytes);

但是任何读取向量大小或向量中任何数据的尝试都失败了。当我使用调试器查看数据时,内存中的数据是正确的,它只是没有与矢量相关联,正如我所希望的那样。

有什么想法吗?我正在尝试做的事情可能吗?

无法在保持内存先前内容的同时为向量分配内存。

工作方法:

  • 根本不要使用 malloc。
  • 使用默认分配器创建具有必要大小的向量。
  • 将二进制文件直接加载到向量中。

假设您无法触及分配部分,因为它位于库的某个深处:只是不要使用向量。您已经有一个动态数组。基于迭代器的算法与指针一起工作得很好。对于基于范围的算法,您需要类似 std::span (C++20) 或类似的东西。

但是使用向量进行分配会更安全,因此更好。

如果您的文件最大为 10 GB,那么我建议改为尝试对文件进行内存映射。映射内存也不能用作向量的存储,因此应采取不使用向量的方法。不幸的是,内存映射文件没有标准方法。