I am trying to write the memory area described by the unique_ptr buffer and uint32_t bufferSize into the std::vector value. When I use std::memcpy for this, I get the following warning:
‘void* memcpy(void*, const void*, size_t)’ writing to an object of type ‘class std::vector<unsigned char, std::allocator >’ with no trivial copy-assignment; use copy-assignment or copy-initialization instead [-Wclass-memaccess]
int ReadByName (std::vector<unsigned char> &value, const char handleName[], std::ostream &out, const long &port, const AmsAddr &server)
{
uint32_t bytesRead;
out << __FUNCTION__ << "(): ";
if (this->handle == 0)
handle = getHandleByName(out, port, server, handleName);
if (bufferSize == 0)
bufferSize = getSymbolSize(out, port, server, handleName);
const auto buffer = std::unique_ptr<uint8_t>(new uint8_t[bufferSize]);
const long status = AdsSyncReadReqEx2( port,
&server,
ADSIGRP_SYM_VALBYHND,
handle,
bufferSize,
buffer.get(),
&bytesRead);
if (status) {
out << "ADS read failed with: " << std::dec << status << '\n';
return 2;
}
value.clear();
value.reserve(bufferSize);
std::memcpy(&value, buffer.get(), bufferSize);
return 0;
}
What would be the correct way to approach the problem?
Regards Tillman
value.assign(buffer.get(), buffer.get() + bufferSize);
? – Jarod42buffer
should be initialized usingstd::unique_ptr<uint8_t[]>(new uint8_t[bufferSize])
rather thanstd::unique_ptr<uint8_t>(new uint8_t[bufferSize])
-- note the extra[]
. – G.M.