c_str回来了,但缺少第一个字符
c_str returned but missing first char
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
}
};
如果您的函数使用具有自动存储持续时间的std::string
,则返回指向其内部存储的指针将产生未定义的行为,因为一旦执行超出范围,对象就会自动销毁(实际字符所在的内存也被释放):
const char* getOutPath()
{
std::string classVarStr;
...
return classVarStr.c_str();
}
这就是为什么返回本地std::string
对象副本(按值)的相同代码按预期工作的原因。
此代码(来自您的编辑):
const char * getAppPath2()
{
return xinstance.getOutPathStr().c_str();
}
调用按值返回对象std::string
getOutPathStr()
,这意味着在 getAppPath2()
方法中,有一个此std::string
对象的副本。但是这个副本也只存在于这个范围内,它相当于:
const char * getAppPath2()
{
std::string copy = xinstance.getOutPathStr();
return copy.c_str();
}
这正是我在开头描述的情况~>UB的原因。
c_str()
返回的 char 指针仅在classVarStr
存在或未更改时才有效。看这里 如果您在该调用后执行了更改字符串的任何操作,则 char 数组将更改。
第二个版本复制字符串,因此更安全。
相关文章:
- 如何忽略第一个字符输入后的所有内容
- 增量生成 - 错误:字符串的第一个字符应'0'或'1'
- 对c++中的输入使用getline函数时忽略第一个字符
- C++如何通过忽略每行的第一个字符来读取 unicode 文件
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- 检索字符串的第一个字符,并在C++中按字母顺序进行比较
- 这个RLE解码功能只解码第一个字符,我做错了什么?
- 如何忽略除第一个字符之外的所有字符
- C++ 如何从用户输入中读取第一个字符并忽略 rest(字符类型)
- CIN.IGNORE删除第一个字符的第二次函数被调用
- 使用CTYPES将字符串从Python传递到C - 仅发送第一个字符
- 为什么在使用循环时忽略输入的第一个字符
- 仅将输入的第一个字符用于 char 变量
- getline删除第一个字符;删除cin.ignore();无法解决
- 仅分配第一个字符,将 LPCTSTR 转换为字符*
- 如何获取指向字符数组中第一个字符的常量指针
- 访问没有字符的字符串的第一个字符
- 如何将 char 类型数组传递给函数并将第一个字符数组复制到第二个图表数组并使用 c++ 进行比较
- 文本文件中的第一个字符会导致 .substr() 错误
- RegSetValueEx 仅显示写入第一个字符