memcpy(c1 + 1 , c1 , 5); 这句的功能应该是把c1处的5字节拷贝到c1+1处,但是我发现,debug版本时是正确的,而release版本时就会出现错误,以下代码将memcpy的结果和memmove的结果做比较,会发现,debug版本时,memcpy和memmove的结果是相同的,而release版的结果会有不同。int _tmain(int argc, _TCHAR* argv[])
{
char c1[21]="12345678901234567890";
char c2[21]="12345678901234567890";
printf("***************************\n c1: %s\n c2: %s\n***************************\n",c1,c2);
for (int i = 1 ; i< 15 ; i ++)
{
memcpy(c1 +1 ,c1,i);
memmove(c2 +1 ,c2,i); if ( 0 != memcmp(c1,c2,20))
{
printf("Different at %d : \n",i);
printf("c1: %s \nc2: %s \n",c1,c2);
}
strcpy( c1, "12345678901234567890" ); // reset string
strcpy( c2, "12345678901234567890" ); // reset string
}
printf("\nPress any key to exit ...");
_getwch();
return 0;
}
{
char c1[21]="12345678901234567890";
char c2[21]="12345678901234567890";
printf("***************************\n c1: %s\n c2: %s\n***************************\n",c1,c2);
for (int i = 1 ; i< 15 ; i ++)
{
memcpy(c1 +1 ,c1,i);
memmove(c2 +1 ,c2,i); if ( 0 != memcmp(c1,c2,20))
{
printf("Different at %d : \n",i);
printf("c1: %s \nc2: %s \n",c1,c2);
}
strcpy( c1, "12345678901234567890" ); // reset string
strcpy( c2, "12345678901234567890" ); // reset string
}
printf("\nPress any key to exit ...");
_getwch();
return 0;
}
解决方案 »
- 各位大老们帮忙啊!!!动态连接库(dll)
- 程序异常之后需要等待x秒才可以再次启动监听端口?
- 继承的控件无法接收消息
- 为什么我的类在执行线程时总是会有0xC0000005Access Violation错误?
- 关于DLLMAIN的问题(30分给回答最满意者)
- 在服务器端执行操作系统命令的问题
- 怎么用ATL生成OCX文件?
- 长角了,散分了,来着有分啊!
- 调查:现在大家所用的中文输入法是什么?录入速度是多少?
- 从文件中读取数据,根据不同的数据在不同的区域显示颜色,比如读出1时,在(1,1,10,10)这个矩形区域显示红色,读出2时,在(10,10,20,20)这个矩形区域显示红色,如何实现,方便的话请举个例子
- 关于Windows音频输入设备切换的问题
- 请问我的对话框菜单为什么不响应快捷键
Microsoft Visual C++ .NET 2003
Windows2000 Professional SP4
***************************
c1: 12345678901234567890
c2: 12345678901234567890
***************************
Different at 2 :
c1: 11145678901234567890
c2: 11245678901234567890
Different at 3 :
c1: 11115678901234567890
c2: 11235678901234567890
Different at 5 :
c1: 11234478901234567890
c2: 11234578901234567890
Different at 6 :
c1: 11234448901234567890
c2: 11234568901234567890
Different at 7 :
c1: 11234444901234567890
c2: 11234567901234567890
Different at 8 :
c1: 11234467801234567890
c2: 11234567801234567890
Different at 9 :
c1: 11234467881234567890
c2: 11234567891234567890
Different at 10 :
c1: 11234467888234567890
c2: 11234567890234567890
Different at 11 :
c1: 11234467888834567890
c2: 11234567890134567890
Different at 12 :
c1: 11234467880124567890
c2: 11234567890124567890
Different at 13 :
c1: 11234467880122567890
c2: 11234567890123567890
Different at 14 :
c1: 11234467880122267890
c2: 11234567890123467890Press any key to exit ...debug 的执行结果:
***************************
c1: 12345678901234567890
c2: 12345678901234567890
***************************Press any key to exit ...
如果把下面的代码中的 +2 改成+1,也有这个问题!!// crt_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";int main( void )
{
printf( "The string: %s\n", str1 );
memcpy( str1 + 2, str1, 4 );
printf( "New string: %s\n", str1 ); strcpy( str1, "aabbcc" ); // reset string printf( "The string: %s\n", str1 );
memmove( str1 + 2, str1, 4 );
printf( "New string: %s\n", str1 );
}
Output
The string: aabbcc
New string: aaaabb
The string: aabbcc
New string: aaaabb
* always handles it correctly; memcpy may handle
* it correctly.
*/一个always,一个may。你有重叠区域,然后用memcpy,文档说了“可能”出问题。然后真出了问题你能说是MS的bug?+1, +2出错与否都无所谓,反正是may。
为什么会
有了memcpy,还要memmove ??然后,看看MSDN对memcpy怎么说的:
Res
The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
但这个恰好不是其中的一个。这个是楼主自己的BUG.
#include <string.h>
#include <stdio.h>char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/* 1 2 3 4 5
* 12345678901234567890123456789012345678901234567890
*/void main( void )
{
printf( "Function:\tmemcpy without overlap\n" );
printf( "Source:\t\t%s\n", string1 + 40 );
printf( "Destination:\t%s\n", string1 + 16 );
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) ); /* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 ); printf( "Function:\tmemmove with overlap\n" );
printf( "Source:\t\t%s\n", string2 + 4 );
printf( "Destination:\t%s\n", string2 + 10 );
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:\t\t%s\n", string2 );
printf( "Length:\t\t%d characters\n\n", strlen( string2 ) ); printf( "Function:\tmemcpy with overlap\n" );
printf( "Source:\t\t%s\n", string1 + 4 );
printf( "Destination:\t%s\n", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}