0
const char* getOutPath() 
{
  return classVarStr.c_str();
}

我有以前的功能,

当我收到返回值时有一些奇怪的东西,

我得到了完整的路径,但不包括第一个字符!

所以如果路径是results/appName_subStr.dat我得到esults/appName_subStr.dat

我将函数调用更改为

string getOutPath() 
{
  return classVarStr;
}

然后我c_str()在收到值后调用以使第一个路径正确char

我猜测它可能会发生,因为函数堆栈弹出可能以某种方式修改了地址?

任何人都遇到过类似的问题,可能是什么原因?

编辑:

class X
{
private:
    string classVarStr;
public:
    X(string in) : classVarStr(in)
    const char* getOutPath() 
    {
      return classVarStr.c_str();
    }
    string getOutPathStr() 
    {
      return classVarStr;
    }
}

class B
{
private:
    X xinstance;
public:
    B(int argc, char * argv[])
    {
         getSomepathFn(argc, argv);
    }

    string getAppPath1() 
    {
        return xinstance.getOutPath(); // this create a string then pass a copy, and internally deleted
    }
    const char * getAppPath2() 
    {
        return xinstance.getOutPathStr().c_str();// this is a problem, create a string, then pass const to the data, and deleted before the function call return, **Undefined behaviour** because the `getOutPathStr()` doesnt return a const reference
    }

}

class appObj
{
     void printMessage()
     {
         B obj = getBObj();
         FILE *fileptr = fopen(obj->getAppPath2(), "a");// this is the faulty area
     }
};
4

2 回答 2

3

如果您的函数使用std::string自动存储持续时间,则返回指向其内部存储的指针将产生未定义的行为,因为一旦执行超出范围,对象就会自动销毁(实际字符所在的内存也被释放):

const char* getOutPath() 
{
    std::string classVarStr;
    ...
    return classVarStr.c_str();
}

这就是为什么返回本地std::string对象副本(按值)的相同代码按预期工作的原因。


此代码(来自您的编辑):

const char * getAppPath2() 
{
    return xinstance.getOutPathStr().c_str();
}

getOutPathStr()按值返回对象的调用std::string,这意味着在getAppPath2()方法内部,有该std::string对象的副本。但是这个副本也只存在于这个范围内,它相当于:

const char * getAppPath2() 
{
    std::string copy = xinstance.getOutPathStr();
    return copy.c_str();
}

这正是我在开头描述的情况〜> UB的原因。

于 2013-10-01T15:46:00.967 回答
1

返回的 char 指针c_str()仅在classVarStr存在或未更改时有效。见这里如果你在调用之后做了任何改变字符串的事情,char 数组将会改变。
第二个版本复制字符串,所以更安全。

于 2013-10-01T15:47:57.807 回答