字符指针未按预期初始化
Char pointer doesn't initialize as expected
MyClass.h
#pragma once
class MyClass {
public:
const char* m_var;
MyClass();
};
MyClass.cpp
#include <iostream>
#include <sstream>
#include "MyClass.h"
MyClass::MyClass() :
m_var(("Text_" + std::to_string(5)).c_str())
{}
主要.cpp
#include <iostream>
#include "MyClass.h"
int main()
{
MyClass myClass;
std::cout << myClass.m_var;
std::cin.get();
}
我原以为程序会输出Text_5
,结果却输出:
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ɽ÷Y╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠⌠ ╠╠╠╠╠╠╠╠
不要这样做。不要试图存储std::string::c_str()
的结果。
子表达式
"Text_" + std::to_string(5)
生成一个临时std::string
对象,该对象在执行完整表达式后立即被自动销毁
("Text_" + std::to_string(5)).c_str()
当该对象被销毁时,c_str()
的结果所指向的C字符串"Text_5"
也被销毁。因此,您的m_var
指针最终指向任何位置。你看到的印刷品是来自"无处"的垃圾。该行为在形式上是未定义的。
例如,这将"按预期"进行
std::cout << ("Text_" + std::to_string(5)).c_str() << std::endl;
// The temporary is destroyed at the end of the full expression.
// It lives long enough to make the above output succeed.
但这不会
const char *str = ("Text_" + std::to_string(5)).c_str();
// The temporary is destroyed at the end of the above expression.
// Here it is already dead, meaning that `str` is an invalid pointer
// and the output below will not work
std::cout << str << std::endl;
这将再次"按预期"工作
std::string s = "Text_" + std::to_string(5);
const char *str = s.c_str();
// Since `s` is not a temporary, it is not destroyed here, `str` remains
// valid and the output below works
std::cout << str << std::endl;
但无论如何,不要试图使用c_str()
的结果进行长期存储。如果你正在处理临时对象,甚至不要考虑存储它。它旨在仅用于一个瞬间,优选地在单个表达式中使用。如果存储c_str()
返回的指针,您可能会突然发现它在没有注意到的情况下变得无效。从形式上讲,只要你真正知道自己在做什么,就可以将指针保留一段时间。但你上面有一个关于它失败的教科书例子。
相关文章:
- 分段错误 - 读取初始化指针的数组
- 初始化指针的常量向量
- 初始化指针或引用成员变量以指向另一个成员
- C++初始化指针/引用/复制细微差别
- 使用无效指针初始化指针声明符的行为是否未定义?
- 在一行中初始化指针(新uint8_t[高度*宽度*3])
- 如何使用可变模板初始化指针?
- 初始化指针时出现写入访问冲突
- C 错误,带有卷曲括号的字面初始化指针
- 重新初始化指针是一种不好的做法吗?
- 初始化指针?C
- 初始化指针向量的向量
- 如何在类函数中正确初始化指针
- C++初始化指针会使应用程序随机崩溃
- 初始化指针结构 - 内存中的外观
- 如何使用struct构造函数在结构中初始化指针阵列
- 在一行中声明并初始化指针到类实例
- 分配数组与初始化指针
- QT 中的非初始化指针
- 使用地址值初始化指针