尝试使用memcpy不断更新数组,但失败了

时间:2014-03-05 17:55:29

标签: c arrays loops dynamic-arrays

只是一些预先信息,下面的代码是我建立的用户空间HID驱动程序的一部分,所以我会尽我所能 跳过与设备连通性有关的部分......

我有两个主要关注的数组,即buf []数组和buffer []数组。 buffer []数组将包含从buf []数组中获取并附加到其上的十六进制值范围。 目的是缓冲区[]基本上保存了大量的字节,然后可以在其他地方使用。

但我似乎遇到缓冲区大小超过1400的问题,我也觉得我的逻辑就像添加一样 到缓冲区是有缺陷的,不像我想的那样工作。我还有另一种方法可以试图实现这个最终目标吗?

如果此代码显示不够,请告诉我,我会进行必要的编辑,我迫切希望得到帮助 对C来说还是很新的。谢谢!

必要时在此处找到完整代码http://pastebin.com/cyawpXCq

int int main(int argc, char const *argv[])
{
unsigned char buf[64];
unsigned char buffer[1400]; //1400 is the max size this should ever be
int i;
size_t buffer_length = 0;

for (i = 0; i < 24; i++)
{
    // in this loop I add elements to the buf[]
    // these elements are sent to the device 
    // which then returns the buf[] with new elements
    // in it. 

    // I then want to push a range of these elements
    // into the buffer which I am attempeting like so
    // I skip the first 7 elements and take the
    // next 16 elements which follow and append those to
    // the buffer[].

    memcpy(buffer + buffer_length, buf + 8, 16);
    printf("\nStored Buffer - \n");
    for (size_t i= 0; i < sizeof(buffer); i++) {
        printf("%02X ", buffer[i]);
    }

    // add another 16 to the length
    buffer_length += 16;

    // loop breaks based on another condition
    break;
}

// This condition comes next and does a similar 
// thing as above

if(true)
{
    // buf[] gets returned to 
    // me containing hex values from a device. I now 
    // want to append these values into the buffer[]
    // and take the 30 elements which come after the 
    // first 7.

    memcpy(buffer + buffer_length, buf + 8, 30);
    printf("\nStored Buffer - \n");
    for (size_t i= 0; i < sizeof(buffer); i++) {
        printf("%02X ", buffer[i]);
    }

    // add another 30 to the length 
    buffer_length += 30;
}

// this loop can can return up to about 200 unique buf[] arrays  
// and again is doing a similar thing where buf[] is filled with device 
// data 
for( int address = 0x0370; address < 0x0370 + 1400 + 1400; address += 28)
{
    // I want to append 28 elements following the 7th element from buf[]
    // to the end of the buffer[] on each iteration

    memcpy(buffer + buffer_length, buf + 8, 28);
    printf("\nStored Buffer - \n");
    for (size_t i= 0; i < sizeof(buffer); i++) {
        printf("%02X ", buffer[i]);
    }

    // add 28 to length each time.
    buffer_length += 28;
    printf("buffer_length = %zu", buffer_length);
}

return 0;
}

0 个答案:

没有答案