下面的代码给我错误***堆栈粉碎被检测到的***:
Below code gives me error *** stack smashing detected ***:
任何人都可以告诉代码中的问题是什么?它为我提供了正确的输出,但最后我收到错误。
我使用GDB检查我在末尾获得信号为
__ stack_chk_fail()在stack_chk_fail.c:2828 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;
}
因为您使用了B数组的B数组,因此固定了分配给堆栈中数组B的内存。如果要避免这种情况,请使char数组足够大,以添加到或尝试使用std :: string并与此链接中的提示相连。如何在C 中加入两个字符串?
std ::字符串将在需要时动态分配内存并增长,如果分配的内存已全部使用。
相关文章:
- 下面是我为检测链接列表中的循环而制作的代码
- OpenCV代码不会检测人脸的数量,也不会在脸周围放一个圆圈
- 堆栈粉碎 在我在代码中添加新变量以及一些操作后C++检测到
- 如何检测 cpp 代码中的 openvino 设备
- 如何以及在何处使用带QT的Basler Cam实现opencv人脸检测代码
- 如何在 Windows API 中检测带有虚拟代码的非字母?
- Cppcheck 静态代码分析器实际上可以检测到不太常见的警告(如 "Relative Path Traversal (CWE-23)" 或"Buffer Under-read(CWE-127)")吗
- 范围检查检测到的检测代码和超出范围的错误访问
- 如何使用Visual Studio检测和获取Windows服务的代码覆盖率?
- 为什么 heob 在 QCreator 生成的代码中将这些检测为内存泄漏
- 如何使用选择正确检测我是在Windows还是Linux中构建C 代码
- Valgrind检测到琐碎代码的未列出的堆内存使用
- 如何从C 代码中检测Intel快速同步的存在
- 如何在编译时检测 C++ 代码中的 SQL 错误
- 引脚类似工具,用于编译时注入检测代码
- 0x00363A09处出现未处理的异常,堆栈cookie检测代码检测到基于堆栈的缓冲区溢出
- 解释在opencv中实现的阴影检测代码的工作原理
- VisualC++ 2010 调试不会检测代码更改
- 用于检测c++代码样式问题的工具
- 如何在开发过程中检测代码重复