char *_xml_ans = new char[1024];
const char *pWlTest  = "<Main><Exception>102</Exception><ExceptionDetail>'/Main/GDC/PassWord' not found </ExceptionDetail></Main>\0";
int len = strlen(pWlTest);
memset(_xml_ans,0,sizeof(_xml_ans)); memcpy(_xml_ans,"1234567890",10);
 
int outlen = strlen(_xml_ans);
TRACE("%d\r\n",outlen);
我这里怎么 总输出 1048 ? 应该显示10才对啊.我的环境是 vc6.0 + XP ,用C++ builder 6.0 就正常显示10 . 

解决方案 »

  1.   


    char *_xml_ans = new char[1024]; 
    const char *pWlTest  = " <Main> <Exception>102 </Exception> <ExceptionDetail>'/Main/GDC/PassWord' not found </ExceptionDetail> </Main>\0"; 
    int len = strlen(pWlTest); 
    memset(_xml_ans,0,sizeof(_xml_ans)); //sizeof(_xml_ans)运算出来的是指针占用的空间,而非其指向内存的空间,所以它为4而非1024.你这一步清空就错了memcpy(_xml_ans,"1234567890",10); int outlen = strlen(_xml_ans); //因为你当初清空不彻底,所以这时候,字符‘\0’是随机的。
    TRACE("%d\r\n",outlen); 
    我这里怎么 总输出 1048 ? 应该显示10才对啊. 
    //正确的做法
    char *_xml_ans = new char[1024]; 
    const char *pWlTest  = " <Main> <Exception>102 </Exception> <ExceptionDetail>'/Main/GDC/PassWord' not found </ExceptionDetail> </Main>\0"; 
    int len = strlen(pWlTest); 
    memset(_xml_ans,0,1024); memcpy(_xml_ans,"1234567890",10); int outlen = strlen(_xml_ans); 
    TRACE("%d\r\n",outlen);