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

问题描述 投票:2回答:1

我正在使用一些较旧的代码,该代码会分配一​​块RAM,然后将一个二进制文件加载到其中。二进制文件是X尺寸为X的一系列8位灰度图像平面,Z深度为Z平面。这些文件通常在500兆字节和10千兆字节之间。

[现有代码使用复杂的指针布置来访问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);

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

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

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

有什么想法吗?我想做的事可能吗?

c++ multidimensional-array stdvector large-files
1个回答
1
投票

在保留存储器的先前内容的同时,不可能将存储器分配给向量。

一种工作方法:

  • 完全不使用malloc。
  • 使用默认分配器创建一个向量,并带有必要的大小。
  • 将二进制文件加载到向量中。
© www.soinside.com 2019 - 2024. All rights reserved.