Copying character array using memcpy

1

Good morning, someone could tell me the difference between

static unsigned char cmd_code[2];
memcpy(&cmd_code, command+offset, 2);

and

static unsigned char cmd_code[2];
memcpy(cmd_code, command+offset, 2);

In both cases the compiler does not give me any errors, however I can not copy the desired characters to cmd_code. What would be the correct way in this case to copy X characters from one character array to another?

    
asked by alxcam8 02.12.2016 в 18:00
source

1 answer

3
static unsigned char cmd_code[2];
memcpy(&cmd_code, command+offset, 2);

cmd_code is a pointer of type char ( char* ). Therefore, when doing &cmd_code you are getting a double pointer ( char** )

memcpy expect to receive a simple pointer, then the first design is wrong and is dangerous.

static unsigned char cmd_code[2];
memcpy(cmd_code, command+offset, 2);

In this case I will assume, because you do not indicate it anywhere, that command is a pointer (I do not care if it is of type char , void , etc.).

In this case, you are copying in cmd_code 2 bytes starting counting at offset of pointer command .

To illustrate an example, if offset was worth 10 the equivalent in simple instructions would be:

cmd_code[0] = command[10];
cmd_code[1] = command[11];

If in this second case you do not copy what you want, the call to the function may be incorrectly configured, but without further details it is impossible to know where the error is.

I edit to indicate why the first example is dangerous.

char c[16];
char* ptr = c;

Imagine that c is in memory location 0x1000 . Then ptr will be found in 0x1010 .

If we do:

memcpy(ptr,"hola",4);

We will be writing in 0x1000 , which is the address pointed to by ptr , that is, we are writing in our buffer c .

If we now change the line and do:

memcpy(&ptr,"hola",4);

Now we are going to write in 0x1010, since it is what we are happening to the function is the memory address where ptr is found. As a result, we will write outside of c , which implies that we will be overwriting values we do not want.

An example:

struct test
{
  char test0; // variable de control
  char c[16];
  char test1; // variable de control
  char* ptr;
  char test2; // variable de control
};

int main()
{
  struct test test;

  // Aquí unicamente estoy inicializando la estructura
  memset(&test,0,sizeof(test));
  test.ptr = test.c;

  memcpy(test.ptr,"abcdefghijklmno",16);
  printf("%s - %i - %i - %i\n",test.c,test.test0,test.test1,test.test2);

  memcpy(&test.ptr,"hhhhhhhhhhhhhhh",16);
  printf("%s - %i - %i - %i\n",test.c,test.test0,test.test1,test.test2);
}

I have declared the variables in a structure to ensure that the compiler leaves them correctly ordered.

In my case, if I execute that code, I get the following result:

abcdefghijklmno - 0 - 0 - 0
abcdefghijklmno - 0 - 0 - 104

If you notice, the variable test2 mysteriously changed in value, while the content of c remains unchanged in the second case. There it is shown that you are treading variables.

    
answered by 02.12.2016 / 21:37
source