Can anyone please tell what is the issue in code? It gives me correct output but at the end i ma getting error " Below code gives me error * stack smashing detected *:"4
I used GDB to check I get signal at the end as
__stack_chk_fail () at stack_chk_fail.c:28 28 stack_chk_fail.c: No such file or directory.
#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;
}