C数组中值的自发修改

时间:2016-04-07 10:03:04

标签: c arrays methods parameter-passing

我有一个奇怪的问题。我有一个while循环,我按值填充数组(在CDC_Receive_HS方法中)。之后,我将此数组作为参数传递给处理方法。但是当我查看在该方法中包含传递数组的值时,与已加载到它的值相比,它们是不同的。我做错了吗?

uint8_t* buffer; 
int counter;

static int8_t CDC_Init_HS(void)
{
  ....

  buffer = (uint8_t*) malloc(sizeof(uint8_t) * 500000);
  counter = 0;

  ....
}   

static int8_t CDC_Receive_HS (uint8_t* Buf, uint32_t *Len)
{
    while(counter < 20000)
    {
        int i = 0;

        for(; i<*Len; i++)
        {
            buffer[counter++] = Buf[i];
        }
        USBD_CDC_ReceivePacket(hUsbDevice_1);
        return (USBD_OK);
    }
  //here are values in "buffer" array ok, so pass it to ParseCommand method
    ParseCommand(buffer, counter);
    return (USBD_OK); 
}

static void ParseCommand (uint8_t* Data, uint32_t Len)
{
  Data[0];
  Data[1];
  ... processing ...
  //Here are values in "Data" array different
}

感谢您的帮助。

0 个答案:

没有答案