C++ 下面的代码给出了错误***检测到堆栈崩溃***:

C++ 下面的代码给出了错误***检测到堆栈崩溃***:,c++,gdb,g++,substring,C++,Gdb,G++,Substring,谁能告诉我代码中的问题是什么? 它为我提供了正确的输出,但在最后,我发现下面的“getting error”代码为我提供了错误*检测到堆栈崩溃*:“4 我用GDB检查我在最后得到的信号 __stack_chk_fail()在stack_chk_fail.c:28 28 stack_chk_fail.c:没有这样的文件或目录 #include <iostream> #include <string.h> #include <iomanip> using nam

谁能告诉我代码中的问题是什么? 它为我提供了正确的输出,但在最后,我发现下面的“getting error”代码为我提供了错误*检测到堆栈崩溃*:“4

我用GDB检查我在最后得到的信号

__stack_chk_fail()在stack_chk_fail.c:28 28 stack_chk_fail.c:没有这样的文件或目录

#include <iostream>
#include <string.h>
#include <iomanip>

using namespace std;

void computeLps (char p[], int n) {
    int *lps = new int[n];
    int len = 0;
    lps[0] = 0;
    int i = 1;
    while(i < n)
    {
        /* code */
        if(p[len] == p[i]){
            len ++;
            lps[i] = len;
            i++;
        }
        else {
            if(len != 0) {
                len = lps[len - 1];
            }
            else{
                lps[i] = 0;
                i++;
            }
        }
    }

    for (int i = 0; i < n; ++i)
    {
        /* code */
        cout << lps[i]<<" ";
    } 
    cout <<endl;
}

int main() {

    char b[] = "ABABDABACDABABCABAB";
    char a[] = "ABABCABAB";

    strcat (b,"$"); 
    strcat (b,a);

    //cout << b;

    computeLps(b,strlen(b));
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
void computeLps(字符p[],整数n){
int*lps=新的int[n];
int len=0;
lps[0]=0;
int i=1;
而(icout因为您为b使用了一个字符数组,所以在堆栈上分配给数组b的内存是固定的。如果要避免这种情况,请将字符数组设置为足够大,以便添加到该数组中,或者尝试使用std::string并与此链接中的提示连接。


STD::String将动态分配内存,如果需要分配内存,它就会增长。

B中没有空间给它添加任何东西。因此,您的StrCAT调用将覆盖其他变量和在STACKE上使用的C++上的数据,卢克。用String。h!Up用字符串!使用STD::String和STD::vector!