vendredi 6 octobre 2023

Facing issue in replacing double pointer variables with vector of pointers inside C++ code

I'm using C++ 11 standard in my code. I have been using a double pointer variable for a while and now I wanted to replace it with a vector of pointers, instead. After replacement, I'm getting segmentation fault errors, and I tried to debug this many times. But, I'm not able to trace out the exact issue.

Initial code:

std::uint8_t** m_outputBuffersDevice{nullptr};
m_outputBuffersDevice = new std::uint8_t* [numOutputBuffers];
for (size_t i = 0; i < numOutputBuffers; i++)
{
  len = **CONSTANT_VALUE**;
  cudaMalloc((void**)&m_outputBuffersDevice[i], len * sizeof(std::uint32_t));
}

Replacement code:

std::vector<std::uint8_t*> m_outputBuffersDevice{nullptr};
m_outputBuffersDevice.resize(numOutputBuffers)
for (size_t i = 0; i < numOutputBuffers; i++)
{
  len = **CONSTANT_VALUE**;
  cudaMalloc((void**)&m_outputBuffersDevice[i], len * sizeof(std::uint32_t));
}

In both versions, numOutputBuffers is the same constant variable. And I'm deallocating the allocated memory using the following statement.

Initial code:

for (size_t i{0}; i < numOutputBuffers; ++i){
   cudaFree(m_outputBuffersDevice[i]);
}
delete[] m_outputBuffersDevice;

Replacement code:

for (size_t i{0}; i < numOutputBuffers; ++i){
   cudaFree(m_outputBuffersDevice[i]);
}

I don't want to use more number of new and delete operations in my code as it is not compliant with C++11 standards. As far as I understood, the resize() operation is just defining the size of the array to which the double pointer is pointing, but it does not allocate any memory. Is this correct? If yes, please suggest any other alternatives to implement the same without (or with a lesser amount) using new and delete operations.

Aucun commentaire:

Enregistrer un commentaire