c++ - 当类A在构造时分配内存时,在类B的构造函数中动态创建vector <class A>

标签 c++ constructor stdvector

我是嵌入式开发人员。现在我是一个使用c++的项目的一部分,因为我们的µC不支持c++,所以我的c++有点生疏了。我希望你们中的一些可以帮助我。

我想用动态数据长度和从站数来表示SPI总线连接。
由于该代码测试了一些用普通C语言编写的µC代码,因此有必要使symmetry-Interface与数组一起使用,而不是与容器一起使用。

这是我声明的类,下面是我的问题的描述。

class LSPI_BusSymulation_SlaveDevice;

class LSPI_BusSymulation{ //Interface to the Symulation-Klass
public:
    LSPI_BusSymulation(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices, uint16_t actualNumberOfDevices);

    // must use normal arrays
    uint16_t* sendSpiData(uint16_t* data, uint16_t dataSize);
private:
    uint16_t actualNumberOfDevices;
    std::vector<LSPI_BusSymulation_SlaveDevice> devices;
};

class LSPI_BusSymulation_SlaveDevice{//Symulates one bus-device, there can be several
    friend LSPI_BusSymulation;
public:
    LSPI_BusSymulation_SlaveDevice(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices);
    ~LSPI_BusSymulation_SlaveDevice();

    // must use normal arrays
    void prepareResponse(uint16_t* data, uint16_t dataSize);
    uint16_t* getBusData();

private:
    // must use normal arrays
    uint16_t* putData(uint16_t* data, uint16_t dataSize);

    uint16_t dataSizePerDevice;
    uint8_t* data;
};

LSPI_BusSymulation类将创建一个 vector ,该 vector 的构造函数中带有ActualNumberOfDevices * LSPI_BusSymulation_SlaveDevice。
如何在不产生内存泄漏的情况下正确实现LSPI_BusSymulation的构造函数?
这是我到目前为止所拥有的。 (其他方法暂时不重要)
LSPI_BusSymulation_SlaveDevice::LSPI_BusSymulation_SlaveDevice(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices)
    : dataSizePerDevice(dataSizePerDevice){
    data = new uint8_t[dataSizePerDevice * (maxNumberOfDevices + 1)];
}

LSPI_BusSymulation_SlaveDevice::~LSPI_BusSymulation_SlaveDevice(){
    delete data;
}

void LSPI_BusSymulation_SlaveDevice::prepareResponse(uint16_t* data, uint16_t dataSize){
    memcpy(this->data, data, dataSize);
}

uint16_t* LSPI_BusSymulation_SlaveDevice::getBusData(){
    return (uint16_t*)this->data;
}

uint16_t* LSPI_BusSymulation_SlaveDevice::putData(uint16_t* data, uint16_t dataSize){
    memcpy(&this->data[this->dataSizePerDevice], data, dataSize);

    return (uint16_t*)this->data;
}

LSPI_BusSymulation::LSPI_BusSymulation(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices, uint16_t actualNumberOfDevices)
    : actualNumberOfDevices(actualNumberOfDevices){
    //create a vector with actualNumberOfDevices * LSPI_BusSymulation_SlaveDevice
    //Does that work, or will I lose the array inside the created class?
    for(int i = 0; i < actualNumberOfDevices; i++)
        devices.push_back(LSPI_BusSymulation_SlaveDevice(dataSizePerDevice, maxNumberOfDevices));
}

uint16_t* LSPI_BusSymulation::sendSpiData(uint16_t* data, uint16_t dataSize){
    uint16_t* retPtr = data;

    for(int i = 0; i < this->actualNumberOfDevices; i++)
        retPtr = devices[i].putData(retPtr, dataSize);

    return retPtr;
}

我看到了很多有关该问题的文章,但是这些文章都没有一个类具有动态分配的内存。而且我不确定这是否有问题。

如果我运行这个简单的main:
int main(){    
    LSPI_BusSymulation test(10, 10, 10);

   return 0;
}

我收到以下错误:
*输入'./a.out'时出错:两次释放或损坏(快速更新):0x0000000000ea0c20 *

希望我提供了所有信息。

最佳答案

如果您使用C++来使用标准容器,则不必担心释放内存。如果要检索原始数组指针,则始终可以访问容器分配的数据。

一些代码修改示例

class LSPI_BusSymulation{
...
private:
    // must use normal arrays
    uint16_t* putData(uint16_t* data, uint16_t dataSize);

    uint16_t dataSizePerDevice;
     std::vector<uint8_t> data;
};

构造函数:
LSPI_BusSymulation_SlaveDevice::LSPI_BusSymulation_SlaveDevice(uint16_t dataSizePerDevice, uint16_t maxNumberOfDevices)
    : dataSizePerDevice(dataSizePerDevice){
    data.resize(dataSizePerDevice * (maxNumberOfDevices + 1));
}

访问数据(示例代码)
uint16_t* LSPI_BusSymulation_SlaveDevice::getBusData(){
    return (uint16_t*)this->data.data(); // access to the container underlying allocated raw data
}

关于c++ - 当类A在构造时分配内存时,在类B的构造函数中动态创建vector <class A>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61609579/

相关文章:

C++ 程序不会打印命令行参数

c++ - 具有这些要求的容器的正确名称是什么?

java - 与 @Builder 结合使用时,我无法让 @RequiredArgsConstructor 工作

c++ - 在移出 vector 上调用 size() 方法是否安全?

c++ - 面试位: Giving run time error on submit but correct output on custom test case

c++ - 如何提高 C++ 中大数 (10^19) 的 pow 精度?

c++ - 如何使用迭代器和用户类正确地制作模板函数

C++ 在另一个类中实例化一组具有相同构造函数的类中的类类型变量

Scala 构造函数弃用

c++ - unique_ptr 的临时只读拷贝