Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm curious as to whether it is safe to pass the data pointer of a QByteArray that has been properly resized, to a C function that will populate the memory within it.

I am thinking that it shouldn't be a problem, but I feel kinda gross doing it.

C++
SpeexBits bits;
void *dec_state;
int bufferPosition = 0;
const int SAMPLES_PER_SEC = 32000;
const int BUFFER_LEN_SECONDS = 5;
const int FRAME_SIZE_BYTES = SAMPLES_PER_SEC / (1000 / 20) * sizeof(int16_t);

QByteArray outBuf;
outBuf.resize(SAMPLES_PER_SEC * sizeof(int16_t) * BUFFER_LEN_SECONDS);

...

QByteArray inBuf((const char*)msg->payload, msg->size);
speex_bits_read_from(&bits, inBuf.data(), inBuf.size());
speex_decode_int(dec_state, &bits, outBuf.data() + bufferPosition);
bufferPosition += FRAME_SIZE_BYTES;

...

<playback>


Though I think this should work, it doesn't feel right, however, I don't want to make multiple copies of the data for no other reason than to populate the QByteArray safely. So I guess my question is, can I safely do this? Is this the recommended way to populate a QByteArray from a function? Is there a better way that doesn't require multiple copies of the data?
Posted
Updated 13-Mar-15 13:05pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900