【发布时间】:2012-12-26 19:21:47
【问题描述】:
我一直在编写一些代码,但遇到了分段错误。我已经尽力让它发挥作用,但我一直失败。现在我正在寻求帮助。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string symbols = "ABCDEFGHIJKLMNOPRSTUVYZ_";
int sim, a = 0;
sim = symbols.size();
string temp[sim];
while(sim > 0)
{
sim--;
temp[sim] = symbols.substr(sim, 1);
cout << temp[sim] << endl;
}
}
嗯,这有点工作,但如果我做一些更改,它会崩溃。
我变了:
while(sim > 0)
{
sim--;
temp[sim] = symbols.substr(sim, 1);
cout << temp[sim] << endl;
}
到这里:
while(a < sim)
{
a++;
temp[a] = symbols.substr(a, 1);
cout << temp[a] << endl;
}
我得到 SIGFAULT,我真的不明白这是错误的。我使用了调试器,它并没有真正显示它在哪一行崩溃,它只是显示这个:
程序收到信号SIGSEGV,分段错误。
在__gnu_cxx::__exchange_and_add(int volatile *, int) () ()
【问题讨论】:
-
你知道程序在什么时候崩溃了吗? (第一次尝试运行,最后等)
-
为了正确起见,拼写为“segfault”。 “分段错误”的缩写。 SIGwhatever 有一个特殊的含义——也就是说,它往往是一个常数,其值代表信号。在这种情况下,拼写为“SIGSEGV”。一个或另一个是合适的,但你必须选择一个。
-
cHao -> 好的,下次我会做的...谢谢解决,我移动了一个++;到循环结束,它工作正常,谢谢大家:)
标签: c++