7 试题
某WIN32程序中采用了动态引用DLL中函数 (函数名foo),但是系统经常提示非法操作内存错误,请问下面哪种方法是正确的不会出错,不必考虑内存泄漏 (正确答案D)
(A)
动态库链接静态C运行库 代码为
void foo(string & strOut)
{
strOut = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
EXE链接动态C运行库 代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo");
string strContent;
(*theFunc) (strContent);
cout << strContent << endl;
(B)
动态库链接静态C运行库 代码为
void foo(string & str)
{
str = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
EXE链接动态C运行库 代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo");
string * pStrContent = new string;
(*theFunc) (* pStrContent);
cout << * pStrContent < delete (pStrContent);
(C)
动态库链接静态C运行库 代码为
void foo(string & strOut)
{
strOut = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
并且DLL提供释放内存函数
void deleteFoo(string *pStrContent)
{
if(pStrContent)
{
delete pStrContent;
}
}
EXE链接动态C运行库 代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo");
DEFFUNC delFunc = GetProcAddress(h," deleteFoo");
string * pStrContent = new string;
(*theFunc)(* pStrContent);
cout << * pStrContent <(*delFunc)( pStrContent);
(D) 动态库链接动态C运行库
void foo(string & strOut)
{
strOut = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
EXE链接动态C运行库 代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo");
string strContent;
(*theFunc) (strContent);
cout << strContent << endl;
阅读(374) | 评论(0) | 转发(0) |