memmove and assign statement

zxm954712 2002-12-12 06:08:57
what's the difference between A and B:
A. memmove(DbgK.label, "BATMAN",sizeof( DbgK.label ));

B. DbgK.label[0] = 'B';
DbgK.label[1] = 'A';
DbgK.label[2] = 'T';
DbgK.label[3] = 'M';
DbgK.label[4] = 'A';
DbgK.label[5] = 'N';
DbgK.label[6] = 0 ;
What I mean is the memory allocation point of view
...全文
45 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
black_snail 2002-12-25
  • 打赏
  • 举报
回复
From Business Logic point of view , There is no difference . But
A is point to Static memory and B is faster .
liushmh 2002-12-13
  • 打赏
  • 举报
回复
效率,A快。
ckacka 2002-12-13
  • 打赏
  • 举报
回复
Example
/* MEMCPY.C: Illustrate overlapping copy: memmove
* always handles it correctly; memcpy may handle
* it correctly.
*/

#include <memory.h>
#include <string.h>
#include <stdio.h>

char str1[7] = "aabbcc";

void main( void )
{
printf( "The string:\t%s\n", str1 );
memcpy( str1 + 2, str1, 4 );
printf( "New string:\t%s\n", str1 );

strcpy( str1, "aabbcc" ); // reset string

printf( "The string:\t%s\n", str1 );
memmove( str1 + 2, str1, 4 );
printf( "New string:\t%s\n", str1 );
}

Output
The string: aabbcc
New string: aaaabb
The string: aabbcc
New string: aaaabb

Remarks
Copies count bytes of characters from src to dest. If some regions of the source area and the destination overlap, memmove ensures that the original source bytes in the overlapping region are copied before being overwritten.


The aboves come from MSDN. Apparently, a function call is different from a evaluation. And the Remarks tells that in this fuction call, there is a series activitis such as "find out overlaping region", "copy the overlaping region".
More, I think, the function call means that there is a string "BATMAN" which is allocated in share memory as a constant variable, then, the call begin. Then this function copy this string to its stack, and run itself... It have too more steps than evaluation.
kvk 2002-12-13
  • 打赏
  • 举报
回复
A快吗,A可是有个判断的!
luixui 2002-12-13
  • 打赏
  • 举报
回复
我认为没有区别
jonepp 2002-12-12
  • 打赏
  • 举报
回复
效率不同
royalier 2002-12-12
  • 打赏
  • 举报
回复
在你这种情况下,是没有什么区别的。但是实际上,从内存来看,还是有区别的。对于A的情况,是早分配了一块内存,里面存的质就是batman。运行完A的这样一句之后,这块内存就变成空的了。但是对于B来说,每个字符还占据着自己的内存。也就是说,对于第一种情况。如果你还想从那块内存中取到batman是不可能得了

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧