kre*_*esh 0 c++ exception segmentation-fault
我正在处理USACO页面上的第一个问题,我收到一条错误消息:
Execution error: Your program had this runtime error:
Exceeded memory or invalid memory reference. The program ran for
0.000 CPU seconds before the error. It used 3372 KB of memory.
Run Code Online (Sandbox Code Playgroud)
我很确定我知道为什么会这样:当我运行我的程序时,它编译并执行正常,但是一旦程序完成,弹出一个框说"main.exe已经停止工作......",然后冻结了.当我运行调试器时,我收到此错误:
an access violation (segmentation fault) raised in your program
.这是什么意思,我该如何解决?我看到了其他几个这样的线程,但我无法使用其他线程的反馈来修复我的程序.这是我的代码:
/*
ID: krishna24
LANG: C++
PROG: ride
*/
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string ride(string comet, string group) {
int cometsum = 1;
int groupsum = 1;
for(int i = 0; i < comet.size(); i++) {
char ascival = comet[i];
cometsum = cometsum*((int)ascival-64);
}
for(int i = 0; i < group.size(); i++) {
char ascival = group[i];
groupsum = groupsum*((int)ascival-64);
}
if (groupsum%47 == cometsum%47) cout << "GO";
else cout << "STAY";
}
int main(int argc, char *argv[])
{
string comet;
getline(cin, comet);
string group;
getline(cin, group);
ride(comet, group);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!
PS:关于如何使我的代码更加简化和高效的任何建议也是有用的.:)
编辑:这不起作用,因为我的ride()
函数没有返回一个字符串.改变它来void
解决问题.感谢大家!
ride
声称要归还string
,但你实际上并没有归还任何东西.我很惊讶编译器让你逃脱了; 你应该启用警告(-Wall
对于海湾合作委员会),甚至可能使警告成为错误(-Werror
对于海湾合作委员会).
归档时间: |
|
查看次数: |
1135 次 |
最近记录: |