-2

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;
  }
MRM
  • 561
  • 5
  • 12
  • 29
  • @Nobody...I don't think it will make a difference since `unsigned char` has 1 byte size. so basically `182 * sizeof(unsigned char)` is same as `182`. – sgarizvi Oct 05 '12 at 07:47
  • I don't get this. Where should that data come from? You define strRecvBuffer to contain one 0 and garbage after that. Other than that, memcpy will copy all of the array, as will your loop. – dbrank0 Oct 05 '12 at 07:47
  • 1
    `= { 0 }` is the syntax to set all elements to 0 – mtsvetkov Oct 05 '12 at 07:48
  • 2
    `memcpy( lpBuffer, strRecvBuffer, 182 )` should do the job. There's something else you're not showing us. Also: why do you cast to `void` sometimes, and not others? Why do you assign `NULL` to a variable which is immediately going out of scope, and why do you check for null before `delete[]` (where a null pointer is legal), and not earlier? For that matter, why do you check for null at all, since `new[]` is guaranteed never to return a null pointer? And perhaps most importantly: why are you using `new[]`, instead of `std::vector< unsigned char >`? – James Kanze Oct 05 '12 at 07:49
  • @dbrank0 the data comes from a serial port, using another method, and the declaration is for initializind all elements with `0`. – MRM Oct 05 '12 at 07:55
  • @JamesKanze what i don't show you is a method that reads the data from a serial port, and returns `strRecvBuffer`. That should not interfere with the rest of the code. – MRM Oct 05 '12 at 07:58
  • @MRM Perhaps, but there's no reason in the code you show for `memcpy` not to work. – James Kanze Oct 05 '12 at 08:10
  • `Debug` set a breakpoint at `memcopy` and check the contents of `lpbuffer` first. – Ujjwal Singh Oct 05 '12 at 08:38
  • @UjjwalSingh did that, and compared the 2 arrays. – MRM Oct 05 '12 at 08:47
  • 1
    @MRM regarding "a method that reads the data from a serial port, and returns strRecvBuffer." in the code above strRecvBuffer is a local variable, therefore we are to assume you are passing it *to* the unseen method? The code above is not the code from your source causing your issue. I've no doubt you have an issue, but in the process of trimming what you deemed unimportant code before posting it here, you trimmed the problem as well. Any doubts to that I suggest you create a stand alone file with just the code above and run *that*. provided both the dyn-alloc and static are 182 bytes. works. – WhozCraig Oct 05 '12 at 09:24
  • @WhozCraig that method was not the problem indeed. I've edited my question with the working code. – MRM Oct 05 '12 at 11:26

1 Answers1

7

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
Chris
  • 2,655
  • 2
  • 18
  • 22
Maxim
  • 1,566
  • 10
  • 13
  • I've tried that also, the result is the same: only the first char is copied. – MRM Oct 05 '12 at 08:17
  • your sample works just fine, applying the method to my code does not work. – MRM Oct 05 '12 at 08:36
  • 1. If my code works fine, than system memcpy works fine 2. When you print unsigned char - be sure to make cast to int (if your code use c++) or printf (for C code): unsigned char var = 12; printf("var is = %d\n", (int) var); – Maxim Oct 05 '12 at 08:44
  • i am sure memcpy works fine, there is still something there, in my code that i do not use right. And it really makes no sense because i the syntax looks fine now. – MRM Oct 05 '12 at 08:48
  • after a a few more looks at my code i realised that: in `BOOL ReceiveSystemState(unsigned char *lpBuffer)`, `lpBuffer` is passed as a pointer to an array of chars, so what the debugger showed my as "only the first char" was indeed the first char from the whole memory zone `lpBuffer` pointed to. (my explanation may not be too accurate). So there was no other problem than ... well, me! I just needed to use `memcpy(&c_dateKG[i], lpBuffer + i * sizeof(unsigned char), sizeof(unsigned char) );` to copy the values of the array back from memory. I updated my post with the corect use of code. Thanks! – MRM Oct 05 '12 at 11:21
  • 2
    The sample Code is wrong! "src" and "dst" in memcpy functions need to be swapped, correct: memcpy(dst, src, sizeof(src)); check the [manual](http://www.cplusplus.com/reference/cstring/memcpy/). The sample code given seems to work because the uninitialized "dst" also contains random charaters. For better testing, use i instead of rand() % 255 in the src initialisation loop. – bhelm May 08 '13 at 06:19
  • error at my previous comment, this is correct: memcpy(dst, src, sizeof(dst)); – bhelm May 08 '13 at 06:59