- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有下面的代码片段。我期待输出将是 mystring
,但奇怪的是它输出垃圾字符。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string s1("mystring");
const char* s2 = s1.c_str();
s1 = "Hi there, this is a changed string s1";
cout << s2 << endl;
return 0;
}
(1)我最初的想法是 c_str
负责分配足够的内存保存 s1
并返回分配给 s2
的内存块的地址,从这里开始 s1
和 s2
独立开始。
(2)但是当我分配 s1 = "Hi there ..... "
时,我在 (1) 中的想法被证明是错误的。不知何故,s1
仍在影响 s2
。
(3)当我注释掉 s1 = "Hi there .... "
行时,一切正常,即 mystring
得到一致打印。
(4)我不太相信我在 (1) 中关于 c_str
正在分配内存来保存 s1
的说法,因为如果是这种情况,我们将不得不处理释放该内存块的问题通过我们不做的 s2
指针。所以我不确定。
请帮我解释一下这个奇怪的行为。
最佳答案
引用 cppreference :
The pointer obtained from
c_str()
may be invalidated by:
- Calling non-const member functions on the string, excluding operator[], at(), front(), back(), begin(), rbegin(), end() and rend().
通过修改字符串,您不再拥有有效的指针。
另外,你的前提是错误的。没有分配/释放内存 w.r.t. c_str
。 c_str
和 data
(在 C++11 中相同)返回指向底层存储的指针。它不拥有存储空间。无论出于何种原因更改存储时,您的指针将不再有效(即,它现在可能指向垃圾)。
关于c++ - c_str 的奇怪行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51893685/
我正在尝试比较 libpqxx c_str值(value)观。 如果我尝试直接比较它们,result1[0][0].c_str() == result2[0][0].c_str(),例如,它们不会 r
const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. c_str()就是把string类对象转换成和c兼容的char *类型。
这个问题在这里已经有了答案: C++ strange behavior with string's c_str() function (2 个答案) 关闭 8 年前。 我得到了以下程序: std::
我想创建一个进程来读取一些串口。但是,用户将来应该能够更改程序所在的路径。这就是为什么我想包含变量 BasePathFile,它在类的初始化过程中设置为默认值: const std::string B
我的 C++ 标准草案拷贝(标记为“ISO/IEC JTC1 SC22 WG21 N3690Date: 2013-05-15") 对 basic_string::c_str() 和 basic_str
我知道 C++11 对 string 做了很多改变。其中最重要的是要求它在内存中线性布局。 在 C++11 之前,对 string::c_str 的调用将返回一个 const char*,但是否保证是
const char* getOutPath() { return classVarStr.c_str(); } 我有之前的功能, 当我收到返回值时有一些奇怪的东西, 我得到完整路径但不包括第一个
这个问题在这里已经有了答案: assigning string::c_str() to a const char* when the string goes out of scope (5 个答案)
如果使用 g++ 和 clang++,我得到 ++my string==my string##my string--。而MSVC和Intel Compiler,则是++==my string##my
我正在做一个 C++ 作业,需要用户输入一个表达式(例如:2 * (6-1) + 2 )并输出结果。除非在用户输入中遇到空格,否则一切正常。 需要将用户输入传递给以下方法; double Calcul
这个问题在这里已经有了答案: Why don't the std::fstream classes take a std::string? (10 个答案) 关闭 8 年前。 我正在阅读《C++ P
这是我在网上找到的一个小型图书馆: const char* GetHandStateBrief(const PostFlopState* state) { static std::ostrin
这个代码片段是正确的还是会导致未定义的行为? std::string s; assert(strlen(s.c_str())==0); 如果不是undefined behavior,上面的断言会通过吗
我已经用普通的 ifstreams 和我正在使用的当前 boost:iostream 尝试了以下代码,两者都有相同的结果。 它旨在将文件从 physfs 加载到内存中,然后将其传递给处理程序进行处理(
我有下面的代码片段。我期待输出将是 mystring,但奇怪的是它输出垃圾字符。 #include #include using namespace std; int main(int argc,
考虑以下函数: void f(const char* str); 假设我想使用 stringstream 生成一个字符串并将其传递给这个函数。如果我想在一个语句中做到这一点,我可能会尝试: f((st
我的理解是 c_str 将一个可能会或可能不会以 null 结尾的字符串转换为以 null 结尾的字符串。 这是真的吗?可以举一些例子吗? 最佳答案 c_str 返回一个 const char*,它指
有时我需要从 C 调用 C++ 对象。经过一些搜索,我知道我可以使用 extern "C" 来包装一些可以从 C 调用的接口(interface)。这是我的代码: #include #include
所以我有一个类 class MySuperClass { public: std::string buffer; }; 并希望将buffer打印到std::cout。 以下是有关从文件填充字符串的一
我从this answer知道字符串文字是静态分配的。但是下面的字符串连接也可以安全使用吗? void someFunction(std::string& foo) { functionTak
我是一名优秀的程序员,十分优秀!