尝试使用memcpy连续更新阵列,但失败
只是一些基本信息,下面的代码是我构建的用户空间HID驱动程序的一部分,所以我会尽我所能 跳过与设备连接等相关的部分 我主要关注两个数组,即buf[]数组和buffer[]数组。 buffer[]数组将由从buf[]数组中获取并附加到其上的十六进制值范围组成。 出于缓冲区[]的目的,缓冲区[]基本上保存了大量字节,这些字节可以在其他地方使用 但我似乎遇到了缓冲区大小超过1400的问题,我也觉得我的逻辑好像是在添加 缓冲区有缺陷,不能像我想的那样工作。有没有其他方法可以让我努力实现这个最终目标 如果这个代码没有显示足够的,请让我知道,我会作出必要的编辑,我迫切需要帮助 对C来说还是很新的。谢谢 如有必要,可在此处找到完整代码尝试使用memcpy连续更新阵列,但失败,c,arrays,loops,dynamic-arrays,C,Arrays,Loops,Dynamic Arrays,只是一些基本信息,下面的代码是我构建的用户空间HID驱动程序的一部分,所以我会尽我所能 跳过与设备连接等相关的部分 我主要关注两个数组,即buf[]数组和buffer[]数组。 buffer[]数组将由从buf[]数组中获取并附加到其上的十六进制值范围组成。 出于缓冲区[]的目的,缓冲区[]基本上保存了大量字节,这些字节可以在其他地方使用 但我似乎遇到了缓冲区大小超过1400的问题,我也觉得我的逻辑好像是在添加 缓冲区有缺陷,不能像我想的那样工作。有没有其他方法可以让我努力实现这个最终目标 如果
int-main(int-argc,char-const*argv[]
{
无符号字符buf[64];
无符号字符缓冲区[1400];//1400是该缓冲区的最大大小
int i;
大小缓冲区长度=0;
对于(i=0;i<24;i++)
{
//在这个循环中,我将元素添加到buf[]
//这些元素被发送到设备
//然后返回带有新元素的buf[]
//在里面。
//然后我想推出一系列这些元素
//进入我正在调度的缓冲区
//我跳过了前7个元素,以
//后面的16个元素,并将其附加到
//缓冲区[]。
memcpy(buffer+buffer_长度,buf+8,16);
printf(“\n存储缓冲区-\n”);
对于(大小i=0;i
单是你的第二个循环就增加了2800个缓冲区长度,当然你已经超过1400个了。@Nick你是怎么知道的?这就是我一直停留在的地方,这可能是显而易见的,但我完全忽略了它。0x0370+1400+1400-0x0370=2800,但增量是28,所以循环迭代100次。在每次迭代中,您将28添加到buffer\u length
。28*100=2800。@Nick谢谢你指出这一点!我使用memcpy的方式对这类事情有意义吗?是的,但你应该在每次使用之前进行边界检查。
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;
}