首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回字符数组函数错误

返回字符数组函数错误
EN

Stack Overflow用户
提问于 2012-08-06 19:07:17
回答 3查看 2.3K关注 0票数 0

可能重复: ” from a function? how to return char array in c++?

这次退货有什么问题吗?我试图使用以下函数返回当前路径,但似乎不正确:

请不要:我需要一个字符,还不是字符串。

代码语言:javascript
复制
char* getINIfile(void)
{
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    string path = string( buffer ).substr( 0, pos) + "\\setup.ini";

    char *ini_local= (char*)path.c_str();

    printf(ini_local); // so far output OK!

    return ini_local;
}

main
{
    printf(getINIfile()); // output Not OK! 

    char mybuffer[200];
    GetPrivateProfileStringA( "files","DLL","0",  mybuffer,200, getINIfile());
    printf(mybuffer);

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-06 19:08:28

当函数退出时,您将返回超出作用域的地址,因此它不再有效:std::string path是函数getINIFile的本地地址,因此在函数退出后它是无效的,从path.c_str()获得的地址也是无效的。

在这种情况下,您可以从函数中返回std::string。如果以后确实需要一个C字符串,那么可以使用c_str()

代码语言:javascript
复制
std::string getINIfile(void)
{
    //...

    return path;
}


int main()
{
    string path = getINIFile();

    // do something with path.c_str():
    const char *cPath = path.c_str();
}

考虑到您的代码,我想不出您必须有一个char*返回的任何原因,但是如果是这样的话,您需要在堆上分配一个缓冲区:

代码语言:javascript
复制
char *getINIfile(void)
{
    char *buffer[MAX_PATH];
    GetModuleFileName(NULL, buffer, MAX_PATH);
    string::size_type pos = string(buffer).find_last_of( "\\/" );
    string path = string(buffer).substr( 0, pos) + "\\setup.ini";

    char *ini_local = new[path.size()];
    strncpy(ini_local, path.c_str(), path.size());

    printf(ini_local); // so far output OK!

    return ini_local;
}

但是这是一个非常糟糕的混合标准C字符串和:只是使用string操作路径,然后在其他地方传递char*

只使用标准C,将find_last_of替换为strrchr --注意缺少错误处理:

代码语言:javascript
复制
char *getINIfile(void)
{
    char *buffer = new[MAX_PATH];
    char *pos = NULL;
    char *ini_local = NULL;

    GetModuleFileName(NULL, buffer, MAX_PATH);
    pos = strrchr(buffer, "\\/");
    // check for and handle pos == NULL

    buffer[pos] = '\0';

    strncat(buffer, "\\setup.ini", MAX_PATH - strlen(buffer));

    printf(buffer);

    return buffer;
}
票数 3
EN

Stack Overflow用户

发布于 2012-08-06 19:09:48

path在函数结束时超出作用域,您将在超出作用域对象中返回一个内部指针。尝试返回一个std::string

代码语言:javascript
复制
std::string getINIfile(void)
{
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    string path = string( buffer ).substr( 0, pos) + "\\setup.ini";

    char *ini_local= (char*)path.c_str();

    printf(ini_local); // so far output OK!

    return path;
}
票数 4
EN

Stack Overflow用户

发布于 2012-08-06 19:08:38

函数正在返回一个指向局部变量的指针,该局部变量超出了作用域,留下了一个悬空指针。为什么不按值返回一个std::string呢?

代码语言:javascript
复制
std::string getINIfile() {
   ....
   return path;
}

然后,只需在调用方使用字符串的底层char*

代码语言:javascript
复制
const std::string s = getINIfile();
const char* c = s.c_str();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11834316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档