为什么不会分配这个值?

时间:2016-01-04 22:48:51

标签: c++ c++11

我有一个成员变量r2,它不会保留分配给它的任何值。该课程的相关部分如下:

m_width

我不断得到的输出:

class GPUFrame
{
private:
    std::shared_ptr<void> m_deviceData;
    unsigned m_pitch = 0;
    unsigned m_width = 2; // testing if m_width will take any value
    unsigned m_height = 0;
    unsigned m_timestamp = 0; // time value in microseconds (absolute value is arbitrary)
    bool m_endOfStream = false; // signifies last frame in the stream

public:

    // make an entirely new allocation
    GPUFrame(unsigned imageWidth, unsigned imageHeight, unsigned allocationCols, unsigned allocationRows,
         unsigned timestamp, bool eos=false)
    {
        // initializer list was causing headaches
        m_pitch = 0;
        m_width = imageWidth;
        m_height = imageHeight;
        m_timestamp = timestamp;
        m_endOfStream = eos;

        // get space from CUDA
        void* newAllocation;
        cudaErr(cudaMallocPitch(&newAllocation, reinterpret_cast<size_t*>(&m_pitch), static_cast<size_t>(allocationCols), static_cast<size_t>(allocationRows)));

        // track allocation with the shared_ptr
        m_deviceData = std::shared_ptr<void>(newAllocation, [=](void* p){ cudaErrNE(cudaFree(p)); });

        std::cout << "imageWidth = " << imageWidth << ", m_width = " << m_width << std::endl;
    }

    // copy from given location
    GPUFrame(CUdeviceptr devPtr, unsigned pitch,
         unsigned imageWidth, unsigned imageHeight, unsigned allocationCols, unsigned allocationRows,
         unsigned timestamp, bool eos=false): GPUFrame(imageWidth, imageHeight, allocationCols, allocationRows, timestamp)
    {
        // copy into a more permanent chunk of memory allocated by above ctor
        cudaErr(cudaMemcpy2D(data(), m_pitch, reinterpret_cast<void*>(devPtr), pitch, allocationCols, allocationRows, cudaMemcpyDeviceToDevice));
    }
}

我很困惑为什么imageWidth = 1920, m_width = 0 甚至会为0,这似乎不是一个选项。有没有人知道我做错了什么?

FWIW,我正在使用m_width选项与g++-5进行编译。完整代码位于https://github.com/briantilley/computer-vision

2 个答案:

答案 0 :(得分:4)

此:

cudaMallocPitch(&newAllocation, reinterpret_cast<size_t*>(&m_pitch), static_cast<size_t>(allocationCols), static_cast<size_t>(allocationRows));

会将音高写入m_pitch。这需要将size_t的数据写入&amp; m_pitch。

m_pitch被声明为&#34; unsigned&#34;。这不一定与size_t的大小相同。如果输出:

std::cout << sizeof(unsigned) << "\n" << sizeof(size_t)

然后我希望你能看到&#34; 4&#34;和&#34; 8&#34;。

所以cudaMallocPitch将写入8个字节,从&amp; m_pitch开始。这将覆盖下一个字段,即m_width。

将m_pitch的类型更改为size_t应解决此问题。您还应该能够删除reinterpret_cast。

答案 1 :(得分:2)

unsigned通常为4个字节。在64位系统上,std::size_t通常为8个字节。 1

cudaMallocPitch写入第二个参数指向的东西,假设它是size_t,但m_pitchunsigned,所以它最终会破坏m_width

1 关于独角兽系统的通常免责声明适用。

相关问题