C++分段错误问题
我的程序崩溃了,这对我来说似乎很好,当然我的程序却说不然,这让我很困惑。
我目前正在处理的函数的这个片段:
for(int k = 0; k < dictionary[k].size(); k++)
{
//"i" represents the fragment's first character
//while "k" represents the dictionary first character
if(fragments[i][j] == dictionary[k][j]) {
token++;
cout << token << endl;
}
}
可能是问题所在。当我调试问题时,调试器会转到代码片段中的第一行:
for(int k = 0; k < dictionary[k].size(); k++)
然后当我尝试转到下一行时崩溃。 在调试器中,此窗口在我的代码块中打开:
Signal Received
Program Received Singal SIGEGV, segmentation fault. Do you want to view backtrace?
我单击“是”,这对我来说似乎是任意的。
有谁知道我做错了什么?
如果需要回溯(窗口显示“调用堆栈”),我将在需要时对其进行编辑
编辑:这是整个函数,我认为这不是必需的
void Language::compare()
{
int para = getParameters(0); //eg. 3
int valid = para;
int token = 0;
for(int i = 0; i < para; i++)
{
//If the string is creater than 2 characters
if(fragments[i].length() > 1) {
for(int j = 0; j < fragments[i].length(); j++)
{
//Checking if that character match in dictionary
for(int k = 0; k < para; k++) //Changed and now works,
{
//"i" represents the fragment's first character
//while "k" represents the dictionary first character
if(fragments[i][j] == dictionary[k][j]) { //But now this line crashes
token++;
cout << token << endl;
}
}
if(token == 0) {
break;
}
}
}
else {
//...
}
}
}
字典和片段在“语言”类中声明” 两者都是向量。
I've gotten a crash in my program which seems fine to me, of course my program says otherwise, which confuses me.
This snippet of my function I am currently working on:
for(int k = 0; k < dictionary[k].size(); k++)
{
//"i" represents the fragment's first character
//while "k" represents the dictionary first character
if(fragments[i][j] == dictionary[k][j]) {
token++;
cout << token << endl;
}
}
might be the problem. When I debug the problem, the debugger goes to the first line in the snippet:
for(int k = 0; k < dictionary[k].size(); k++)
then crashes when I try go to the next.
In the debugger, this window opens up in my Code Blocks:
Signal Received
Program Received Singal SIGEGV, segmentation fault. Do you want to view backtrace?
I clicked yes and it just seems arbitrary to me.
Does anyone know what I've done wrong?
If the backtrace is needed(the window says Call Stack), I'll edit it on later if needed
EDIT: This is the whole function, which I've thought it would not be necessary
void Language::compare()
{
int para = getParameters(0); //eg. 3
int valid = para;
int token = 0;
for(int i = 0; i < para; i++)
{
//If the string is creater than 2 characters
if(fragments[i].length() > 1) {
for(int j = 0; j < fragments[i].length(); j++)
{
//Checking if that character match in dictionary
for(int k = 0; k < para; k++) //Changed and now works,
{
//"i" represents the fragment's first character
//while "k" represents the dictionary first character
if(fragments[i][j] == dictionary[k][j]) { //But now this line crashes
token++;
cout << token << endl;
}
}
if(token == 0) {
break;
}
}
}
else {
//...
}
}
}
dictionary and fragments is declared in the class "Language" which are both vector.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑您打算使用
dictionary[k].size()
作为循环控制的一部分,因为循环正在对k
进行迭代。您可能指的是dictionary.size()
或dictionary[i].size()
吗?I doubt you intended to use
dictionary[k].size()
as part of your loop control there, since the loop is iterating overk
. Did you meandictionary.size()
ordictionary[i].size()
perhaps?这句话:
确实看起来很可疑。您确定不想循环到字典数组/集合的大小吗?
如果您发布了
dictionary
的定义,它可能会帮助我们给出一些具体的建议。This line:
does seem suspect. Are you sure you don't want to loop up to the size of the dictionary array/collection?
If you posted the definition of
dictionary
it might help us to give some concrete suggestions.在尝试取消引用之前,您应该确保
dictionary[k].size()>j
字典[k][j]
。You should make sure
dictionary[k].size()>j
before you attempt to dereferencedictionary[k][j]
.进入循环时 j 的值是多少?
我同意其他人的观点:
for(int k = 0; k
似乎是假的。 这样的东西
像for(int k = 0; k
似乎更有意义。
What is the value of j on entry to the loop?
And I agree with the other guys:
for(int k = 0; k < dictionary[k].size(); k++)
seems bogus. Something like
for(int k = 0; k < dictionary[m].size(); k++)
would seem to make more sense.
关于 for 循环中的测试的其他评论看起来很可疑,可能是正确的,但此外,如果您第一次尝试进入循环时遇到此段错误,那么您可能正在使用字典
向量
没有条目。在这种情况下,dictionary[0].size() 具有未定义的行为(这很可能是段错误)。
The other comments about the test in the for loop looking suspect are probably correct, but in addition if you're getting this segfault the first time you try to go into the loop then it's possible that you're working with a
dictionary
vector<>
that has no entries.In this case
dictionary[0].size()
has undefined behavior (which might well be a segfault).