stack - *** 检测到堆栈粉碎 *** : a. 终止

标签 stack stack-smash

因此,我正在编程 I 课上编写一个程序,我们被分配创建一个程序来检查银行的​​路由号码是否有效。每次我在 NetBeans 上运行该程序时,都没有出现任何问题。 但是,当我通过 Ubuntu 学生服务器运行它时,我收到了这个疯狂的错误。我已经尝试调试这个程序一段时间了,但不太明白这个错误是从哪里来的。我尝试过更改数组大小,但没有成功,我仍然收到此错误。这是错误的副本:

Enter a bank routing number: 
103000648
1 0 3 0 0 0 6 4 8 
7 3 9 7 3 9 7 3 
7 0 27 0 0 0 42 12 
7 7 34 34 34 34 76 88 
88
8
the check code is valid
*** stack smashing detected ***: a.out terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f28e6be8e57]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x0)[0x7f28e6be8e20]
a.out[0x400f99]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f28e6b0076d]
a.out[0x400b39]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:11 40895235                           /home/wm/wm130/a.out
00601000-00602000 r--p 00001000 08:11 40895235                           /home/wm/wm130/a.out
00602000-00603000 rw-p 00002000 08:11 40895235                           /home/wm/wm130/a.out
01ffd000-0202f000 rw-p 00000000 00:00 0                                  [heap]
7f28e67e3000-7f28e68de000 r-xp 00000000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e68de000-7f28e6add000 ---p 000fb000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e6add000-7f28e6ade000 r--p 000fa000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e6ade000-7f28e6adf000 rw-p 000fb000 08:01 5505592                    /lib/x86_64-linux-gnu/libm-2.15.so
7f28e6adf000-7f28e6c93000 r-xp 00000000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6c93000-7f28e6e92000 ---p 001b4000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6e92000-7f28e6e96000 r--p 001b3000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6e96000-7f28e6e98000 rw-p 001b7000 08:01 5508613                    /lib/x86_64-linux-gnu/libc-2.15.so
7f28e6e98000-7f28e6e9d000 rw-p 00000000 00:00 0 
7f28e6e9d000-7f28e6eb3000 r-xp 00000000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e6eb3000-7f28e70b2000 ---p 00016000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e70b2000-7f28e70b3000 r--p 00015000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e70b3000-7f28e70b4000 rw-p 00016000 08:01 5505594                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f28e70b4000-7f28e71b6000 r-xp 00000000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e71b6000-7f28e73b5000 ---p 00102000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e73b5000-7f28e73bd000 r--p 00101000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e73bd000-7f28e73bf000 rw-p 00109000 08:01 34866318                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f28e73bf000-7f28e73c2000 rw-p 00000000 00:00 0 
7f28e73c2000-7f28e73e4000 r-xp 00000000 08:01 5505877                    /lib/x86_64-linux-gnu/ld-2.15.so
7f28e75c5000-7f28e75ca000 rw-p 00000000 00:00 0 
7f28e75df000-7f28e75e4000 rw-p 00000000 00:00 0 
7f28e75e4000-7f28e75e5000 r--p 00022000 08:01 5505877                    /lib/x86_64-linux-gnu/ld-2.15.so
7f28e75e5000-7f28e75e7000 rw-p 00023000 08:01 5505877                    /lib/x86_64-linux-gnu/ld-2.15.so
7ffffd60a000-7ffffd62b000 rw-p 00000000 00:00 0                          [stack]
7ffffd676000-7ffffd677000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

这是我的源代码的副本:

#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <string.h>

using namespace std;

int main() {

    string code;

    int routing[9];
    int multipliers[8] = {7, 3, 9, 7, 3, 9, 7, 3};
    int subtotals = 0;
    int products[8];
    int sum = 0;

    cout << "Enter a bank routing number: " << endl;
    cin >> code;

    char ch_code[8];
    strcpy(ch_code, code.c_str()); //store character code into array

    for (int i = 0; i < 9; i++) { //convert string to integer and print routing
        routing[i] = ch_code[i] - '0';
        cout << routing[i] << " ";
    } cout << endl;

    for (int i = 0; i < 8; i++) { //print multipliers
        cout << multipliers[i] << " ";
    } cout << endl;

    for (int i = 0; i < 8; i++) { // print and calculate products
        products[i] = routing[i] * multipliers[i];
        cout << products[i] << " ";
    } cout << endl;

    for (int i = 0; i < 8; i++) { // calculate subtotals and print
        subtotals += products[i];
        cout << subtotals << " ";
    } cout << endl;

    cout << subtotals << endl; // print sum
    cout << subtotals % 10 << endl; // check digit

    if(subtotals % 10 == routing[8]) {
        cout << "the check code is valid" << endl;
    }

    else cout << "the check code is invalid" << endl;
}

如果您熟悉此错误消息,请帮忙!非常感谢!

最佳答案

ch_code 缓冲区已被输入溢出。

关于stack - *** 检测到堆栈粉碎 *** : a. 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191514/

相关文章:

c - 当我访问超出我分配的内存时,为什么我没有收到堆栈粉碎错误?

c++ - 有没有办法在进程中捕获堆栈溢出? C++ Linux

c++ - 结构构造函数问题导致堆栈溢出

python - 如何使用检查从绑定(bind)方法中获取类名?

c++11 - C++11中有没有更好的方法在堆栈上构造类

c - 使用C中的fgets()函数检测到堆栈崩溃

c - 堆栈实现中的引用调用错误(C程序)

c++ - 读取功能的段错误

c - 通过接受用户输入覆盖内存中的值

c - execv() 系统调用创建堆叠粉碎错误