从数组到具有动态内存分配的新数组的memcopy

时间:2017-03-23 14:47:52

标签: c++ arrays dynamic-memory-allocation memcpy

我正在尝试使用一些额外的标头数据创建一个新数据包。我有旧的数据包缓冲区。我想为VLAN ID添加新的标头标记。这是我的方法。

//This is old buffer with values here .
u8_t* oldpacket = new u8_t[oldPacketLength];

所以我创建了一个新的缓冲区,用于插入旧数据+4个额外的头字节

#define ETH_LENGTH_OF_ADDRESS 6
u32_t newPacketLength = oldPacketLength + 4 ;
u32_t remainSize = oldPacketLength-(2 * ETH_LENGTH_OF_ADDRESS);

u8_t* newpacket = NULL;
newpacket = new u8_t[newPacketLength ];

//get ethernet data upto dst mac & src MAC 
PPF_ETHERNET_HEADER pEthHeader = (PPF_ETHERNET_HEADER) pPacket;

//insert these MAC data to new packet
NdisMoveMemory(newpacket , &pEthHeader->aDstMac, 6);
NdisMoveMemory(newpacket + 6 , &pEthHeader->aSrcMac, 6);

//insert ID 0x8100 to new packet
PUSHORT pTpid;
Tpid = (PUSHORT)((PUCHAR)newpacket + 2 * ETH_LENGTH_OF_ADDRESS);
*Tpid = 802TPID;
TagHeader = (PVLAN_TAG_HEADER)(Tpid + 1);

//Insert remaining old data to new pcket
NdisMoveMemory(newpacket + (2 * ETH_LENGTH_OF_ADDRESS) + 4 ,oldpacket + (2 * ETH_LENGTH_OF_ADDRESS),remainSize );

我运行时会得到不同的值。 NdisMoveMemory中的任何错误?

0 个答案:

没有答案