c++ - Is it possible to get a raw pointer to std::vector<unsigned int> data? -
this question has answer here:
- how std::vector pointer raw data? 3 answers
i have std::vector filled instructions proprietary hardware. have code construct buffer , manipulate fine.
the problem need make use of library (c based) builds similar proprietary hardware instructions, library expects make raw memory available , return pointer it, library uses write commands.
i accomplish following [pseudo code, nothing real code]:
void* thelibrarycallback (void* clientdata, unsigned int thelibrarysize) { std::vector<unsigned int> cmd = reinterpret_cast<std::vector<unsigned int>>(clientdata); std::vector<unsigned int>::size_type cmdsize = cmd->size(); (int loop=0;loop<thelibrarysize; loop++) cmd->push_back(0xdeadbeef); return cmd->get_raw_pointer_at(sizeof(unsigned int)*cmdsize); ///<< how can this? }
the concept add values vector make space, other library modifies them directly.
if cannot ill have consider allocating temporary raw memory, filling out library , copying vector, avoid if possible.
is want possible somehow?
so 2 things:
you can access contiguous memory of vector
std::vector::data()
or&vec.front()
.if in code shown above, you'll returning pointer data disappears when function returns (the vector's destructor run, , data freed).
Comments
Post a Comment