简体   繁体   中英

MEX C++ raw data access

I'm trying to access the raw data of a MATLAB matrix passed to a MEX C++ function based on the answer here . Strangely, I get a memory access error every time. How do I access the raw data?

void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs)
{
    TypedArray<uint32_t> dataArray = std::move(inputs[0]);
    uint32_t* dataRaw = dataArray.release().get();

    for (auto& elem : dataArray)
    {
        elem *= 2; // Works
        uint32_t x = (*dataRaw); // Memory access error
        ++dataRaw;
    }
[...]
}

The problem was solved by storing the pointer returned from the release() method explicitly. It seems that the data is lost otherwise.

TypedArray<uint32_t> dataArray = std::move(inputs[0]);
auto dataPtr = dataArray.release();
uint32_t* dataRaw = dataPtr.get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM