在 c++ 中的 memcpy 之后获取 strlen
我正在尝试使用 memcpy 函数连接字符,但是,经过几次 memcpy 后,我的缓冲区长度有点奇怪。请参阅
int main()
{
uint8 txbuffer[13]={0};
uint8 uibuffer[4] = "abc";
uint8 rxbuffer[4] = "def";
uint8 l[2]="g";
int index = 1;
cout << strlen((char*)txbuffer) <<endl;
memcpy(&txbuffer[1],&uibuffer, strlen((char*)uibuffer));
index+=strlen((char*)uibuffer);
cout <<"after first memcpy: "<< strlen((char*)txbuffer) <<endl;
memcpy(&txbuffer[index],&rxbuffer, strlen((char*)uibuffer));
cout <<"after second memcpy: "<< strlen((char*)txbuffer) <<endl;
memcpy(&txbuffer[0],&l, strlen((char*)l));
cout <<"after third memcpy: "<< strlen((char*)txbuffer) <<endl;
for (int i = 0; i < sizeof(txbuffer); i += 1)
{
cout << (int(txbuffer[i]))<<" : "<< char(int(txbuffer[i]))<<endl;
}
return 0;
}
输出下面的代码:
after first memcpy: 0
after second memcpy: 0
after third memcpy: 7
103 : g
97 : a
98 : b
99 : c
100 : d
101 : e
102 : f
0 :
0 :
0 :
0 :
0 :
0 :
我的问题是为什么在第一个 memcpy 之后,缓冲区的 strlen 仍然为零?
i`m trying to concatenate a characters using memcpy function, however, i kinda get a weird length of my buffer after couple of memcpy. please see code below
int main()
{
uint8 txbuffer[13]={0};
uint8 uibuffer[4] = "abc";
uint8 rxbuffer[4] = "def";
uint8 l[2]="g";
int index = 1;
cout << strlen((char*)txbuffer) <<endl;
memcpy(&txbuffer[1],&uibuffer, strlen((char*)uibuffer));
index+=strlen((char*)uibuffer);
cout <<"after first memcpy: "<< strlen((char*)txbuffer) <<endl;
memcpy(&txbuffer[index],&rxbuffer, strlen((char*)uibuffer));
cout <<"after second memcpy: "<< strlen((char*)txbuffer) <<endl;
memcpy(&txbuffer[0],&l, strlen((char*)l));
cout <<"after third memcpy: "<< strlen((char*)txbuffer) <<endl;
for (int i = 0; i < sizeof(txbuffer); i += 1)
{
cout << (int(txbuffer[i]))<<" : "<< char(int(txbuffer[i]))<<endl;
}
return 0;
}
the output is:
after first memcpy: 0
after second memcpy: 0
after third memcpy: 7
103 : g
97 : a
98 : b
99 : c
100 : d
101 : e
102 : f
0 :
0 :
0 :
0 :
0 :
0 :
my question is why after the first memcpy, the strlen of the buffer still is zero?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在从原始文件中进行
memcpy
ingstrlen
字节之后,您不应该strlen
目标,因为您没有复制 0 终止符。此外,您从字节 1 开始复制,而不是 0,这意味着 strlen 是 0,因为您的数组最初被归零(这使得我的第一段变得无关紧要,但您应该意识到这一点)。
You should not
strlen
destination aftermemcpy
ingstrlen
bytes from the original, because you didn't copy the 0-terminator.Besides, you copy starting at byte 1, not 0, meaning
strlen
is 0, because your array is initially zeroed (that kind of makes my first paragraph irrelevant, but you should be aware of it).这是因为
txbuffer
中的第一个字符是空字符\0
。 (您以这种方式初始化它。)因此,当您打印出来时,该字符串实际上具有零长度。您没有覆盖第一个或第二个副本中的第一个字符。但你最终在第三个副本中覆盖了它。这就是为什么在第三次复制之前长度为零的原因。
This is because the first character in
txbuffer
is the null character\0
. (You initialized it this way.) So the string effectively has zero-length when you print it out.You didn't overwrite the first character in the first or second copies. But you finally do overwrite it in the 3rd copy. That's why the length is zero until after the 3rd copy.
strlen()
计算非 NUL 字节的数量。由于初始 NUL 在复制后仍然保留,因此strlen()
当然返回零。相反,您可能更喜欢使用
sizeof
或一些更明确的串联逻辑:strlen()
counts the number of non-NUL bytes. Since the initial NUL remains after the copy, of coursestrlen()
returns zero.Instead, you might prefer using
sizeof
or some more explicit concatenation logic:这里有多个问题。
首先使用 &uibuffer 作为 memcpy 的参数是错误的,只需使用 uibuffer (rxbuffer,l) 因为它们已经是一个地址(是数组):
在上面的 txbuffer 上执行 strlen (因为你复制到带有偏移量 1) 的 txtbuffer 将产生 0 长度,因为 strlen 会计数直到找到 \0,将 strlen 与 memcpy 结合使用并不是一个好主意,而是跟踪长度您手动在 txtbuffer 中,您知道复制的字符串的长度和偏移量。
there are multiple issues here.
first of all using &uibuffer as argument to memcpy is wrong, just use
uibuffer (rxbuffer,l)
since they are already an addresses (being arrays):doing a strlen on the above txbuffer (since you copy to the txtbuffer with offset 1) will yield 0 length since strlen counts until it finds \0, using strlen in combination with memcpy isn't a good idea instead keep track of the length you have in txtbuffer manually, you know the lengths of the strings you copy in and the offsets.