memcpy这个函数使用有前提么,我在程序中用他时,提示access voilation at address xxxxxxxx,不知这是为什么。
请教高手!!!!
请教高手!!!!
解决方案 »
- 弱弱地问一个很菜的问题,如何在VC的调试界面下绘制抛物线?以及一些简单的曲线?
- 想看看 自己建的mfc doc/view 工程中类别型录网中每个元素的名字 怎么不行
- 求助:能不能设计一个包含多线程的类啊?
- 打开表的小问题,在线等...
- gethostbyaddr 失败的原因有哪些?
- 拜师学艺!!!
- 我写了一个静态函数的程序,总是不对。请帮我改改吧。谢谢!非常简单
- CString的public方法为什么不能访问他的私有成员?
- 两个小问题
- 请教:如何安全并完全地终止一个线程?
- 奇怪的问题,编译时选择使用Use MFC in a Static Library程序一运行就异常了,在线等待中......
- 熟悉Property sheets消息的朋友请进,请教问题?
char string1[] = "long long a ago" ;
char string2[] = "aaaa bbbb c ddd" ;
memcpy( string1+5, string2+5, 4 ) ;
string1:就会是 long bbbb a ago ;
出错原因可能是你要加上头文件#include <stdio.h>
Copies characters between buffers.void *memcpy( void *dest, const void *src, size_t count );Routine Required Header Compatibility
memcpy <memory.h> or <string.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.LibrariesLIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Valuememcpy returns the value of dest.ParametersdestNew buffersrcBuffer to copy fromcountNumber of characters to copyResThe 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.Example/* MEMCPY.C: Illustrate overlapping copy: memmove
* handles it correctly; memcpy does not.
*/#include <memory.h>
#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 ) );
}
OutputFunction: memcpy without overlap
Source: fox
Destination: dog jumps over the lazy fox
Result: The quick brown fox jumps over the lazy fox
Length: 43 charactersFunction: memmove with overlap
Source: quick brown fox jumps over the lazy dog
Destination: brown fox jumps over the lazy dog
Result: The quick quick brown fox jumps over the lazy dog
Length: 49 charactersFunction: memcpy with overlap
Source: quick brown dog jumps over the lazy fox
Destination: brown dog jumps over the lazy fox
Result: The quick quick brown dog jumps over the lazy fox
Length: 49 characters
char *a="asdfasdfasdfasdf";
char *b="";
memcpy(b,a,3);
就不对,但
char a[100]="asdfasdfasdfasdf";
char b[100]="";
memcpy(b,a,3);
就可以,难道a本身不是地址么
而char a[100]已经分配了100字符长的地址了。
若程序改为:
char *a="asdfasdfasdfasdf"
char b[]="";
memcpy(b,a,3);
没有问题.说明不是a有问题,是b有问题到底问题在哪里?我跟踪调试你原来的程序,发现到memcpy.asm中的
mov [edi],al ;U - put first byte into destination
出现access violation
可见b的地址必须确定!这才是错误的根源.所以,正确改动如下:
char *a="asdfasdfasdfasdf";
char *b=new char;
memcpy(b,a,3);
这样的话,p指向的是执行文件image中某个read-only的内存页面,你试图用memcpy写入这个地址的时候就会发生异常。char p[]="asdfasdfasdfasdf";
这样p是一个在栈中分配的数组(如果不是全局变量的话),写入当然没有问题。