调试-编程接收到的信号SIGSEGV,分段错误 [英] Debug---Program received signal SIGSEGV, Segmentation fault

查看:645
本文介绍了调试-编程接收到的信号SIGSEGV,分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在这里,由G ++编译.输入N = 600000后输入.弹出一个窗口,说明 *.exe已停止工作.调试代码时,标题为 Error 的窗口表示程序在排序行时收到信号SIGSEGV,Segmentation fault..现在,我知道这是stackoverflow错误.谢谢大家!

The code is here, and it is compiled by G++. After input N=600000 and enter. A window popped out said that *.exe has stopped working. When debugging the code, a window titled Error said that Program received signal SIGSEGV, Segmentation fault. when come to sort line. Now, I know it's stackoverflow error.Thank you, all guys!

#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <algorithm>

int main (int argc, char *argv[]) {

int N;
std::cout << "N:";
int a[N];
for(int i = 0; i < N; i++){
    a[i] = rand()%N;
}
std::sort(a,a+N);
std::cout << "The "<<N/2<<"th smallest number is: " << a[N/2-1] <<"\n";
return 0;
}

推荐答案

据我所知,具有运行时大小的数组是C99的功能,仅在某些C ++编译器中作为扩展提供.将其切换为std::vector<int> a(N);,代码应该可以正常工作.

As far as I know, an array with a run-time size is a C99 feature and only provided as extension in some C++ compilers. Switch this to a std::vector<int> a(N); and the code should work.

顺便说一句:

  • 排序不是找到第N个最小数字的最快方法.
  • 请考虑克里斯的评论,您的问题缺少必要的信息.另外,缩进并清理代码,我认为没有必要运行循环.
  • 您还可以从IOStreams检索输入,无需使用scanf():while (cin >> N) { /*use n*/ }.
  • Sorting this is not the fastest way to find the N-th smallest number.
  • Please consider Chris' comment, that your question lacks the necessary information. Also, indent and clean up your code, I don't think that it's necessary to run a loop.
  • You can also retrieve input from IOStreams, no need to use scanf(): while (cin >> N) { /*use n*/ }.

这篇关于调试-编程接收到的信号SIGSEGV,分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆