Question

I need to pass an unsigned char array from one method to another, and i tried using this code:

{
     unsigned char *lpBuffer = new unsigned char[182];
 ReceiveSystemState(lpBuffer);

}

BOOL ReceiveSystemState(unsigned char *lpBuffer)
  {
      unsigned char strRecvBuffer[182] = { 0 };
      //strRecvBuffer construction

      memcpy(lpBuffer, strRecvBuffer, sizeof(strRecvBuffer));

      return TRUE;
 }

Neither of those 3 methods (used in ReceiveSystemState) worked as i expected. After using each one of them all that it is copied is the first char from strRecvBuffer and nothing more. The strRecvBuffer has empty chars from element to element, but i need those as they are, because that string is a message from a hardware device and that message will be anallysed using a protocol. What do i miss here? Do i initialize lpBuffer wrong?

EDIT: I've used a simple memcpy method to do the job. Still the same result: all that it is copied is the first char of strRecvBuffer.

EDIT2: Working code:

{
     unsigned char *lpBuffer = new unsigned char[182];
     ReceiveSystemState(lpBuffer);
     for (int i = 0; i < 144; i++)
     {
         memcpy(&c_dateKG[i], lpBuffer + i * sizeof(unsigned char), sizeof(unsigned char) );
     }

}    
  BOOL ReceiveSystemState(unsigned char *lpBuffer)
  {
     unsigned char strRecvBuffer[182] = { 0 };
     //strRecvBuffer construction

     memcpy(lpBuffer, strRecvBuffer, sizeof(strRecvBuffer));

     return TRUE;
  }
Was it helpful?

Solution

Your code is absolutely garbage. Some notes:

Use sizeof:

Use sizeof(static_massive_name); or count_of_arr_elements * sizeof(arr_type);

For example:

unsigned char src[255];    
unsigned char dst[255];

// fill src with random data
for (int i = 0; i < 255; ++i) {
    src[i] = static_cast<unsigned char> (rand() % 255);
}

memcpy(dst, src, sizeof(dst));


// now dst will have copied values from src (random numbers)

UPDATE:

Full source code for testing:

#include <iostream>
#include <string.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

void print(unsigned char* arr, size_t size) {
    for (size_t i = 0; i < size; ++i) { 
        // see type casting (to int)!!!
        cout << "arr[" << i << "] = " << (int)arr[i]<< endl;
    }
}

int main() {
    srand(time(0));

    // unsigned char type hold values from 0 to 255
    unsigned char src[15];
    unsigned char dst[15];

    for (int i = 0; i < 15; ++i) {
        src[i] = rand() % 255;
    }

    memcpy(dst, src, sizeof(dst));

    print(src, 15);
    print(dst, 15);

    return 0;
}

Result:

arr[0] = 34
arr[1] = 80
arr[2] = 183
arr[3] = 112
arr[4] = 18
arr[5] = 120
arr[6] = 183
arr[7] = 0
arr[8] = 0
arr[9] = 0
arr[10] = 0
arr[11] = 57
arr[12] = 137
arr[13] = 4
arr[14] = 8
arr[0] = 34
arr[1] = 80
arr[2] = 183
arr[3] = 112
arr[4] = 18
arr[5] = 120
arr[6] = 183
arr[7] = 0
arr[8] = 0
arr[9] = 0
arr[10] = 0
arr[11] = 57
arr[12] = 137
arr[13] = 4
arr[14] = 8
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top