用string.h将错误分段到队列中
segmentation faults with string.h into queue
很抱歉,noob问题无法确定在这里使用哪些函数。http://www.cplusplus.com/reference/string/string/
我本来打算转换成c字符串并写一大堆代码,但我敢打赌有一个好方法可以做到这一点。
只需尝试将A、B和C附加到字符串的末尾并将其添加到队列中,就可以不断获得以A结尾的分段错误tho??字符串::assign()后的()函数(根据调试器)
string a, b, c;
a = s.append("A");
b = s.append("B");
c = s.append("C");
q.add(a);
q.add(b);
q.add(c);
这也以分段错误结束。
q.add(s + "A");
q.add(s + "B");
q.add(s + "C");
还有一个问题是它使用了旧的,所以我会得到:
teststringA
teststringAB
teststringABC
而不是预期的
teststringA
teststringB
teststringC
什么是分段错误
当程序运行时,它可以访问内存的某些部分。首先,在每个函数中都有局部变量;这些存储在堆栈中。其次,您可能有一些在运行时分配的内存(在C中使用malloc,或者在C++中使用new)存储在堆上(您也可能听说它被称为"自由存储")。您的程序只允许触摸属于它的内存——前面提到的内存。任何超出该区域的访问都将导致分段故障。分段故障通常被称为分段故障。
你的第二个问题是
q.add(s + "A"); // appends A to s hence teststringA
q.add(s + "B"); // teststringA + B hence teststringAB
q.add(s + "C"); //teststringAB + C hence teststringABC
请参阅http://www.cplusplus.com/reference/string/string/append/
Append to string
The current string content is extended by adding an additional appending string at its end.
The arguments passed to the function determine this appending string:
string& append ( const string& str );
Appends a copy of str.
示例
// appending to string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
string str2="Writing ";
string str3="print 10 and then 5 more";
// used in the same order as described above:
str.append(str2); // "Writing "
str.append(str3,6,3); // "10 "
str.append("dots are cool",5); // "dots "
str.append("here: "); // "here: "
str.append(10,'.'); // ".........."
str.append(str3.begin()+8,str3.end()); // " and then 5 more"
str.append<int>(5,0x2E); // "....."
cout << str << endl;
return 0;
}
输出:
Writing 10 dots here: .......... and then 5 more.....
相关文章:
- 分段故障(堆芯转储)矢量
- boost::进程间消息队列引发错误
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 数组的指针从不分段故障
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- Android NDK传感器向事件队列报告奇怪的间隔
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 在线编译器中的分段C++没有打印消息
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 前缀表示法 C++,分段错误(堆栈和队列)
- 线程安全队列出现分段错误
- 了解从队列弹出时引用中的分段错误
- C++类内的队列,分段错误
- 使用推送时队列程序出现分段错误
- 用string.h将错误分段到队列中
- 第二次推送时,优先级队列出现自定义比较器分段故障
- 实现队列类,分段错误
- 用户定义指针队列中的分段错误