wchar_t*  unicoudToMuil(const char* cha)
{
wchar_t* tt = NULL;
int len = MultiByteToWideChar(CP_ACP,0,cha,-1,NULL,0);
wchar_t* gg = new wchar_t[len];
MultiByteToWideChar(CP_ACP,0,cha,-1,gg,len);
tt = gg; delete[] gg;
return tt;
}
在我的程序里
...wchar_t* tt = null;//这里我不想每次都new出来tttt = unicoudToMuil("测试");//tt这里是个乱值,请问应该如何解决
...

解决方案 »

  1.   

    tt = unicoudToMuil(_T("测试")); 
      

  2.   

    不行,因为我是测试我可以直接写,我实际应用的时候是个变量
    const char* mm;tt = unicoudToMuil(mm); //这里是个变量,不是常量
      

  3.   

    delete[] gg; 你已经delete掉了。在VC调试时调试器会用0xCD来填充。
      

  4.   

    下面的应该可以
    wchar_t*  unicoudToMuil(const char* cha) 

    static wchar_t* tt = NULL; 
    if(tt)
    {
      delete[] tt;
    }
    int len = MultiByteToWideChar(CP_ACP,0,cha,-1,NULL,0); 
    wchar_t* tt = new wchar_t[len]; 
    MultiByteToWideChar(CP_ACP,0,cha,-1,tt,len); 
     
    return tt; 
      

  5.   

    不想每次都分配可以让外面传进来,比如
    wchar_t* unicoudToMuil(const char* cha, wchar_t *output, int len);或者干脆就用string,wstring得了。
      

  6.   

    上面的有个小bug,改正如下
    wchar_t*  unicoudToMuil(const char* cha) 

    static wchar_t* tt = NULL; 
    if(tt) 

      delete[] tt; 

    int len = MultiByteToWideChar(CP_ACP,0,cha,-1,NULL,0); 
    tt = new wchar_t[len]; 
    MultiByteToWideChar(CP_ACP,0,cha,-1,tt,len); return tt; 
      

  7.   

    wchar_t*  unicoudToMuil(const char* cha) 

    wchar_t* tt = NULL; 
    int len = MultiByteToWideChar(CP_ACP,0,cha,-1,NULL,0); 
    wchar_t* gg = new wchar_t[len]; 
    MultiByteToWideChar(CP_ACP,0,cha,-1,gg,len); 
    tt = gg; delete[] gg; 
    return tt; 
    } 指针所指向的内存都已经delete了,返回的tt不出错才怪。
      

  8.   

    gg都被你干掉了,当然tt没有了。