分段故障11:GDB原因13
Segmentation fault 11: gdb reason 13
这是我的代码:
int main(){
long userInput;
long placeArray[userInput +1];
long divisionPlace, modPlace;
long number = userInput;
long two = 2;
cout << "enter: ";
cin >> userInput;
for (int i = 1; i < userInput; i++) {
divisionPlace = number / two;
modPlace = number % two;
placeArray[number - i ] = modPlace;
}
for (int i = 0; i < userInput; i++) {
cout << placeArray[i] << " ";
}
cout <<endl;
return 0;
}
有人可以指出我在代码中的错误,说明我为什么要记忆不正确?
如评论中所述,您在此之前使用userInput
:
long placeArray[userInput +1];
因此,当您在下面的循环中访问它时,placeArray
不会像您期望的那样大小。这将导致写入您没有分配的记忆,并且会弄乱您的堆栈。
您的数组分配不正确。
long userInput;
cout << "enter: ";
cin >> userInput;
if (userInput <= 0)
{
cerr << "error" << endl;
exit(1);
}
long* placeArray = new long[userInput +1];
long divisionPlace, modPlace;
long number = userInput;
long two = 2;
for (int i = 1; i < userInput; i++) {
divisionPlace = number / two;
modPlace = number % two;
placeArray[number - i ] = modPlace;
}
for (int i = 0; i < userInput; i++) {
cout << placeArray[i] << " ";
}
cout <<endl;
delete [] placeArray;
相关文章:
- 当在带有Eigen的C++中使用GDB时,我如何才能看到更多的大矩阵
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- LNK2038、MSVS2017 MAGMA的原因列表
- 为什么它不适用于Visual 2019的原因
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数
- 我无法缩小此分段错误的原因
- 链表中写入访问冲突的未知原因
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 使用vscode调试时,GDB意外退出
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 从VS 2015更新3更新到VS2015更新3 d后浮点计算行为不同的原因
- 如何找出gdb停止的原因
- 分段故障11:GDB原因13
- GDB - 确定分段错误的原因
- 当应用程序没有从gdb运行时,我能得到应用程序崩溃的原因吗
- gdb运行我的程序和bash运行我的程序显示不同输出的原因是什么呢?