需要有关此memcpy和数组代码的帮助 [英] Need help with this memcpy and array code
问题描述
我想在主函数中合并2个数组.但是由于某些原因,即使我可以使array3 [0]和array3 [1]具有正确的值,但是当它们复制到数据包中时,它们的值是不同的.知道为什么吗?
感谢您的帮助.
I want to merge the 2 arrays in the main function. But for some reason, even though I can get array3[0] and array3[1] to have the right values, when they are copied into the packet, their values are different. Any idea why ?
Thanks for your help.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned char *function1()
{
unsigned char array2[] = { 0x4a,0xb2 };
return (array2 );
}
main()
{
unsigned char temp[] = { 0xaa, 0x0b, 0x03,0x04,0x05,0x06,0x07,0x08,0x09 };
unsigned char x[2];
unsigned char *packet;
int pkt_len;
pkt_len = sizeof(temp) + sizeof(x);
packet = (unsigned char*) malloc ( pkt_len +1);
memset( packet, 0x00, pkt_len +1);
unsigned char *pointer1 = malloc ( sizeof(temp) + 1);
memset( pointer1, 0x00, sizeof(temp) +1);
memcpy (pointer1, temp, sizeof(temp) );
memcpy (packet, pointer1, sizeof(temp) );
printf("\nPacket before copy is 0x%x\n", packet[8]);
unsigned char *array2 = malloc ( sizeof (x) + 1) ;
array2 = (char *)function1();
printf("\nArray2 is 0x%x\n", array2[0]);
memcpy (packet + sizeof(temp), array2, sizeof(x) );
printf("After copy, Packet contents are 0x%x\n", packet[9]);
}
推荐答案
首先:您确定要从function1()返回局部变量的地址吗?函数返回时,没有保证array2将指向有意义的内存块.
其次:您确定您的程序是有效的C吗?我不再是C方面的专家,但是在C89中完全声明,在不声明语句之后声明变量. array2和pointer1是我正在查看的变量.
第三:您正在进行大量的复制和内存设置,因此很难知道您实际上打算做什么.例如:
First off: Are you sure you want to return an address of a local variable from function1()? There''s no guarentee that array2 will point to a meaningful block of memory when the function returns.
Secondly: Are you sure your program is valid C? I''m not an expert on C anymore but declaring variables after non declaring statements was a complete no-no in C89. array2 and pointer1 are the variables I''m looking at in particular.
Thirdly: You''ve got loads of redundant copying and memsetting going on so it''s a bit hard to see what you''re actually intending to do. For example:
memset( pointer1, 0x00, sizeof(temp) +1);
memcpy (pointer1, temp, sizeof(temp) );
可以替换为:
could be replaced with:
memcpy ( pointer1, temp, sizeof temp );
pointer1[ sizeof temp] = 0x00;
第四:在C语言中,您无需强制转换malloc的返回值. void *与任何指针类型兼容.
第五:每当有经验的C程序员看到sizeof(x)时,他们便开始寻找称为x的类型.取一个对象的大小时,不要使用花括号.
第六:指针1的要点是什么?似乎完全是多余的.
如果您只想动态创建一个内存块并将其他两个数组复制到其中,则可以用大约三行代码完成:
-分配内存
-将第一个数组复制到分配的内存中
-将第二个数组复制到第一个数组之后的已分配内存中
干杯,
灰
Fourthly: In C you don''t need to cast the return value of malloc. void * is compatible with any pointer type.
Fifthly: Whenever an experienced C programmer sees sizeof( x ) they start looking for a type called x. Leave off the braces when taking the size of an object.
Sixth: What''s the point of pointer1? It seems totally redundant.
If all you want to is dynamically create a lump of memory and copy two other arrays into it you can do it in about three lines:
- allocate the memory
- copy the first array into the allocated memory
- copy the second array into the allocated memory after the first array
Cheers,
Ash
这篇关于需要有关此memcpy和数组代码的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!