字符指针未按预期初始化

Char pointer doesn't initialize as expected

本文关键字:初始化 指针 字符      更新时间:2023-10-16

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()返回的指针,您可能会突然发现它在没有注意到的情况下变得无效。从形式上讲,只要你真正知道自己在做什么,就可以将指针保留一段时间。但你上面有一个关于它失败的教科书例子。