CString变量的最大长度不是2G吗?但我这样写
CString strTest("" , nLen);
这个nLen大于64K左右时会运行出错。我糊涂了

解决方案 »

  1.   

    不死才怪。
    CString strTest("" , nLen);
    调用的是
    CString(LPCSTR lpch, int nLength); 
    后面的参数表示lpch的长度,你这个""长度为0,你给了64K,都读到U盘了。
      

  2.   

    CString 不用指定内存长度的
      

  3.   

    读到U盘了?
    pch 
    A pointer to an array of characters of length nLength, not null-terminated我只是开了一个64K个BYTE长度的CString而已啊。MSDN 上的例子:
    CAtlString s6( 'x', 6 );          // s6 = "xxxxxx"
      

  4.   

    我要指定长度是为了在后面调用GETBUFFER()进行数据拷贝的。
      

  5.   

    何必呢?
    数据copy,就用CString.Fromat来做了,方便简单
      

  6.   

    不一定能分配那么大的空间啊……我想应该是这样的。
    int nLength = str.GetLength();
    如果是2G的空间的话,不死才怪呢!
    赫赫
      

  7.   

    CStringT inherits from CSimpleStringT Class. Advanced features, such as character manipulation, ordering, and searching, are implemented by CStringT.Note   CStringT objects are capable of throwing exceptions. This occurs when a CStringT object runs out of memory for any reason.
      

  8.   

    下面是MSDN的source code,看来64K跨段了,不能用这个构造函数
    CString::CString(LPCTSTR lpch, int nLength)
    {
    Init();
    if (nLength != 0)
    {
    ASSERT(AfxIsValidAddress(lpch, nLength, FALSE));
    AllocBuffer(nLength);
    memcpy(m_pchData, lpch, nLength*sizeof(TCHAR));
    }
    }
      

  9.   

    ""的类型是const char[1],会匹配const char *的构造函数,而不是你认为的char。你可以如下:
    CString str('\0', 64 * 1024);
      

  10.   

    to sjhunter():
      64k跨段了,什么意思,你以为还在DOS啊。
    to ilovevc(ilovevc)
       你写错了。
      CString strTest("" , nLen);第一个参数LPCSTR长度为零,nLen有什么意思那,当然死了。
    写成 CString strTest('1',64000)或者第一个参数LPCSTR长度为64k 就可以了。
      

  11.   

    CString str('\0', 64 * 1024);确实是对的。CString的构造函数不存在64K跨段问题,只是
    CString strTest("" , 64000);这样有问题。我认为ilovevc的话没错。但我还是不太明白第一位兄弟所言:读到U盘了.
      

  12.   

    假设我有一个字符串hello,world
    我想将CString初始化为hello,只要前面的5个字符,那么可以
    CString str("hello,world", 5);
    然后CString将从前面读5个字符,变成“hello",现在你给了一个"",实际长度算上结束字符才为1,然后欺骗CString说这个字符串至少有64k,那么就读出界了,也就是我说的都已经读到“u盘”了。
      

  13.   

    使用CString( TCHAR ch, int nRepeat = 1 );构造函数,如果使用CString( LPCTSTR lpch, int nLength );那么nLength的长度要小于等于LPCTSTR lpch;
      

  14.   

    Anikan(皮皮鱼)
    不是什么DOS不DOS的问题,ASSERT(AfxIsValidAddress(lpch, nLength, FALSE));这句话死了,因为从内存地址lpch开始后64k已经是???,就是无效地址了,关键在于搂主想用CString('', 64*1024)来构造CString, 却将''错写成了"",如果nLength小,那么从lpch开始的nLength这么长的地址空间都是应用程序自己的,就不会死,太大就死了