This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320F28335: Problem with C-Library function memcpy

Part Number: TMS320F28335


Tool/software: TI C/C++ Compiler

I am seeing a odd issue when using memcpy routine, from <string.h> standard c library.  I am trying to copy data from one array to another, whose types are uint32_t .  I am seeing that if i use memcpy to do this, as below  it only copies half the array.

static uint32_t vehicle_key[8] = {0x55555555U, 0x55555555U, 0x55555555U, 0x55555555U, 0x55555555U, 0x55555555U, 0x55555555U, 0x55555555U};

memcpy(vehicle_key_dest, vehicle_key, 8);

But, if i do it by using a for loop (as shown below), then it works as expected. 

for ( i = 0; i < 8; i++ )
{
   vehicle_key_dest[i] = vehicle_key[i];
}

Does anyone know why i would be seeing different behavior from these functions?

  • Hi,

    memcpy() size argument specifies memory size, not array length. You need to use sizeof() operator to get size of specific object:

    memcpy(vehicle_key_dest, vehicle_key, sizeof(vehicle_key_dest));

    or if extern declaration of vehicle_key_dest doesn't specify array length, then perhaps this way

    memcpy(vehicle_key_dest, vehicle_key, sizeof(vehicle_key_dest[0]) * 8);

    Edward
  • ah, i should have none that. Thanks for quick response