关于在函数中定义局部指针变量问题
当多次调用这函数时,char * strBuff;申请的内存地址都一样,有什么办法,使每次调用函数时,申请的内存地址不同吗?
cout<<strBuff;会出现异常,这是类中一个函数,用vs2010编译的
char* register_Name_MySql::GetIniInfo(char* Section,char * Key){ char * strBuff; GetPrivateProfileStringA(Section,Key,NULL,strBuff,32,IniPath); cout<<strBuff; return strBuff;}
char* register_Name_MySql::GetIniInfo(char* Section,char * Key){ char * strBuff=new char [32];//我这里写的32是为了和你GetPrivateProfileStringA第五个参数一致,你实际要多少自己确定... GetPrivateProfileStringA(Section,Key,NULL,strBuff,32,IniPath); cout<<strBuff; return strBuff;}用完strBuff之后要delete[]strBuff;
[解决办法]
这里还是用智能指针管理strbuff更好,不然都不知道在哪去delete[]strbuff
[解决办法]
按下面修改就行了。
用new申请的空间,如果不手动删除,会一直到程序运行结束有系统释放。
char* register_Name_MySql::GetIniInfo(char* Section,char * Key)
{
char * strBuff = new char[50]; // 所需的大小假设50
GetPrivateProfileStringA(Section,Key,NULL,strBuff,32,IniPath);
cout<<strBuff;
return strBuff;
}
[解决办法]
DWORD WINAPI GetPrivateProfileString( __in LPCTSTR lpAppName, __in LPCTSTR lpKeyName, __in LPCTSTR lpDefault, __out LPTSTR lpReturnedString, __in DWORD nSize, __in LPCTSTR lpFileName);