I want my function to return a BYTE array. The function is as follows.
BYTE sendRecieveData(BYTE control, unsigned int value){
//Open connection to LAC
HANDLE LACOutpipe;
HANDLE LACInpipe;
LACOutpipe=openConnection(MP_WRITE);
LACInpipe=openConnection(MP_READ);
//declare variables
BYTE bufDataOut[3];
BYTE bufDataIn[3];
DWORD bufInProcess;
DWORD bufOutProcess;
//sets CONTROL
bufDataOut[0]=control;
//sets DATA to be sent to LAC
BYTE low_byte = 0xff & value;
BYTE high_byte = value >> 8;
bufDataOut[1]=low_byte;
bufDataOut[2]=high_byte;
MPUSBWrite(LACOutpipe,bufDataOut,3,&bufOutProcess,1000);
MPUSBRead(LACInpipe,bufDataIn,3,&bufInProcess,1000);
MPUSBClose(LACOutpipe);
MPUSBClose(LACInpipe);
return bufDataIn[3];
}
It doesn’t return a byte array and when I change BYTE to BYTE[] or BYTE[3] it gives me an error.
return bufDataIn[3];means “return 4th element ofbufDataInarray” and in this case it leads to undefined behaviour since the size of this array is 3.You can allocate memory for this new array in the body of your function and return pointer to its first element:
Don’t forget to
deleteit when you finish with it:Even better, use
std::vector<BYTE>and get rid of this ugly memory management with it 😉 This ensures that the memory is properly freed on any return path, even when exceptions are thrown.