C 程序执行在代码块中终止

C 程序执行在代码块中终止,c,codeblocks,C,Codeblocks,我已经编写了一个程序来测量快速排序的执行时间,并且正在使用代码块来编译和运行它。当我输入一组随机数时,我的程序对于任何大小的数据都运行良好。但当我尝试输入升序/降序排序的数字集时,我的程序终止,因为有一个大数据集(>35000),表示程序停止工作 同样的代码在linux上运行良好。但在linux上,我无法使用QueryPerformanceCounter()测量时间。所以我必须在windows上使用代码块 #include<stdio.h> #include<windows.h

我已经编写了一个程序来测量快速排序的执行时间,并且正在使用代码块来编译和运行它。当我输入一组随机数时,我的程序对于任何大小的数据都运行良好。但当我尝试输入升序/降序排序的数字集时,我的程序终止,因为有一个大数据集(>35000),表示程序停止工作

同样的代码在linux上运行良好。但在linux上,我无法使用QueryPerformanceCounter()测量时间。所以我必须在windows上使用代码块

#include<stdio.h>
#include<windows.h>
double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    printf("QueryPerformanceFrequency failed!\n");

    PCFreq = (double)li.QuadPart;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return (double)(li.QuadPart-CounterStart)/PCFreq;
}
int part(int a[],int low,int high)
{
    int pivot,i,j,temp;
    pivot=low;
    i=low+1;
    j=high;
    while(i<=j)
    {
        while(a[i]<=a[pivot])
            i++;
        while(a[j]>a[pivot])
            j--;
        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
    temp=a[j];
    a[j]=a[pivot];
    a[pivot]=temp;
    return j;
}
void QuickSort(int a[],int first,int last)
{
    int q;
    if(first<last)
    {
        q=part(a,first,last);
        QuickSort(a,first,q-1);
        QuickSort(a,q+1,last);
    }
}

int main()
{
    int n,a[100000],i;
    printf("Enter the size of array:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        //a[i]=rand()%100000;   //average case (random data)
        a[i]=i;                 //ascending sorted input
        //a[i]=n-1-i;           //descending sorted input
    /*
    printf("The UNsorted array is:\n");
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
    printf("\n\n");
    */

    StartCounter();
    QuickSort(a,0,n-1);
    printf("Sorting time %lf micro seconds\n",GetCounter()*1000000.0);

    /*
    printf("\nThe sorted array is:\n");
    for(i=0;i<n;i++)
        printf("%d ",a[i]);
    printf("\n");
    */
    return 0;
}
#包括
#包括
双PCFreq=0.0;
__int64 countstart=0;
void StartCounter()
{
大整数李;
if(!QueryPerformanceFrequency(&li))
printf(“QueryPerformanceFrequency失败!\n”);
PCFreq=(双)li.QuadPart;
QueryPerformanceCounter(&li);
反启动=li.QuadPart;
}
双GetCounter()
{
大整数李;
QueryPerformanceCounter(&li);
返回(双)(li.四部分反启动)/PCFreq;
}
整数部分(整数a[],整数低,整数高)
{
内枢轴,i,j,温度;
枢轴=低;
i=低+1;
j=高;
而(i如果我在Linux上使用
clock(3)
(足够的分辨率用于快速测试),它将达到并包括
100000
的固定限制。为了避免这个固定限制,我使用
malloc()
在下面的草图中动态保留内存(结果没有什么不同,我只是想把它放在堆上而不是堆栈上)

(可重复) 我认为递归有点太深了(这里的堆栈大小:8兆,符合180,00次调用,有两个整数、一个指针和一些开销)

所以:这里没有什么问题

编辑 为了证明它确实是堆栈,让我们增加它:

#include <time.h>
#include <stdlib.h>
#include <sys/resource.h>
    // ALL CHECKS OMMITTED!
int main()
{
  int n, *a, i;
  clock_t start, stop;
  // 256 MB
  const rlim_t bigger_stack = 256L * 1024L * 1024L;
  struct rlimit rlim;
  int ret_set, ret_get;

  ret_get = getrlimit(RLIMIT_STACK, &rlim);
  fprintf(stderr, "get %d %d %d\n", ret_get, (int) rlim.rlim_max,
      (int) bigger_stack);
  if (ret_get == 0) {
    if (rlim.rlim_cur < bigger_stack) {
      rlim.rlim_cur = bigger_stack;
      ret_set = setrlimit(RLIMIT_STACK, &rlim);
      if (ret_set != 0) {
    fprintf(stderr, "getrlimit %d, setrlimit %d\n", ret_get, ret_set);
      }
    }
  }



  printf("Enter the size of array:");
  scanf("%d", &n);
  a = malloc(sizeof(int) * n);

  for (i = 0; i < n; i++)
    //a[i]=rand()%100000;   //average case (random data)
    a[i] = i;           //ascending sorted input
  //a[i]=n-1-i;           //descending sorted input
  /*
   * printf("The UNsorted array is:\n");
   * for(i=0;i<n;i++)
   * printf("%d ",a[i]);
   * printf("\n\n");
   */

  start = clock();
  QuickSort(a, 0, n - 1);
  stop = clock();
  printf("Sorting time %f seconds\n", (double) (stop - start) / CLOCKS_PER_SEC);
  /*
   * printf("\nThe sorted array is:\n");
   * for(i=0;i<n;i++)
   * printf("%d ",a[i]);
   * printf("\n");
   */
  free(a);
  return 0;
}
#包括
#包括
#包括
//所有支票都兑现了!
int main()
{
int n,*a,i;
时钟没有启动,停止;
//256 MB
常量rlim t更大的堆栈=256L*1024L*1024L;
结构rlimit-rlim;
int ret_set,ret_get;
ret_get=getrlimit(RLIMIT_堆栈,&rlim);
fprintf(stderr,“获取%d%d\n”,ret_get,(int)rlim.rlim_max,
(int)更大的_堆栈);
如果(ret_get==0){
if(rlim.rlim\u cur<更大的堆栈){
rlim.rlim_cur=更大的_堆栈;
ret_set=setrlimit(RLIMIT_堆栈,&rlim);
如果(重新设置!=0){
fprintf(stderr,“getrlimit%d,setrlimit%d\n”,ret_get,ret_set);
}
}
}
printf(“输入数组的大小:”);
scanf(“%d”和“&n”);
a=malloc(sizeof(int)*n);
对于(i=0;i时钟(3)
(足够的分辨率用于快速测试),它将达到并包括
100000
的固定限制。为了避免该固定限制,我使用
malloc()
在下面的草图中动态保留内存(结果没有什么不同,我只是想把它放在堆上而不是堆栈上)

(可重复) 我认为递归有点太深了(这里的堆栈大小:8兆,符合180,00次调用,有两个整数、一个指针和一些开销)

所以:这里没有什么问题

编辑 为了证明它确实是堆栈,让我们增加它:

#include <time.h>
#include <stdlib.h>
#include <sys/resource.h>
    // ALL CHECKS OMMITTED!
int main()
{
  int n, *a, i;
  clock_t start, stop;
  // 256 MB
  const rlim_t bigger_stack = 256L * 1024L * 1024L;
  struct rlimit rlim;
  int ret_set, ret_get;

  ret_get = getrlimit(RLIMIT_STACK, &rlim);
  fprintf(stderr, "get %d %d %d\n", ret_get, (int) rlim.rlim_max,
      (int) bigger_stack);
  if (ret_get == 0) {
    if (rlim.rlim_cur < bigger_stack) {
      rlim.rlim_cur = bigger_stack;
      ret_set = setrlimit(RLIMIT_STACK, &rlim);
      if (ret_set != 0) {
    fprintf(stderr, "getrlimit %d, setrlimit %d\n", ret_get, ret_set);
      }
    }
  }



  printf("Enter the size of array:");
  scanf("%d", &n);
  a = malloc(sizeof(int) * n);

  for (i = 0; i < n; i++)
    //a[i]=rand()%100000;   //average case (random data)
    a[i] = i;           //ascending sorted input
  //a[i]=n-1-i;           //descending sorted input
  /*
   * printf("The UNsorted array is:\n");
   * for(i=0;i<n;i++)
   * printf("%d ",a[i]);
   * printf("\n\n");
   */

  start = clock();
  QuickSort(a, 0, n - 1);
  stop = clock();
  printf("Sorting time %f seconds\n", (double) (stop - start) / CLOCKS_PER_SEC);
  /*
   * printf("\nThe sorted array is:\n");
   * for(i=0;i<n;i++)
   * printf("%d ",a[i]);
   * printf("\n");
   */
  free(a);
  return 0;
}
#包括
#包括
#包括
//所有支票都兑现了!
int main()
{
int n,*a,i;
时钟没有启动,停止;
//256 MB
常量rlim t更大的堆栈=256L*1024L*1024L;
结构rlimit-rlim;
int ret_set,ret_get;
ret_get=getrlimit(RLIMIT_堆栈,&rlim);
fprintf(stderr,“获取%d%d\n”,ret_get,(int)rlim.rlim_max,
(int)更大的_堆栈);
如果(ret_get==0){
if(rlim.rlim\u cur<更大的堆栈){
rlim.rlim_cur=更大的_堆栈;
ret_set=setrlimit(RLIMIT_堆栈,&rlim);
如果(重新设置!=0){
fprintf(stderr,“getrlimit%d,setrlimit%d\n”,ret_get,ret_set);
}
}
}
printf(“输入数组的大小:”);
scanf(“%d”和“&n”);
a=malloc(sizeof(int)*n);
对于(i=0;i*对于(i=0;i)我们是否不想将malloc()返回的值类型强制转换为指针变量的类型?我试图使用malloc/calloc显式分配内存,但仍然面临相同的问题。上面的代码在windows上无法处理代码块。不,我们不应该对
malloc()的输出进行cat
正好相反。另外:堆(分配有
malloc()
)不是问题,堆栈是(太多的递归)。什么“不工作”确切地说是什么意思?我的意思是:编译器/链接器/操作系统等有什么错误。请复制并粘贴错误,不要转述。我认为
getrlimit()
et al.在Windows中有不同的名称,但这只是为了证明堆栈(太多的递归)是罪魁祸首。CodeBlock提供了自己的方法来增加堆栈,例如:如果我不强制转换malloc()的返回值,它会给出一条错误消息说“错误:从'void*'到'int*'的转换无效”发布生成错误的代码。注释没有任何意义。(
void
)是一个通用指针,可以从任何其他类型转换到任何其他类型(这是类型或目的…)。不,您不会转换
malloc
(或
calloc
)的返回值请参阅:以获得详细解释。我们不应该将malloc()返回的值类型强制转换为指针变量的类型吗?我尝试使用malloc/calloc显式分配内存,但仍然面临相同的问题。上面的代码在windows上无法处理代码块。不,我们不应该对
malloc()的输出进行cat
正好相反。另外:堆(使用
malloc()分配)
#include <time.h>
#include <stdlib.h>
#include <sys/resource.h>
    // ALL CHECKS OMMITTED!
int main()
{
  int n, *a, i;
  clock_t start, stop;
  // 256 MB
  const rlim_t bigger_stack = 256L * 1024L * 1024L;
  struct rlimit rlim;
  int ret_set, ret_get;

  ret_get = getrlimit(RLIMIT_STACK, &rlim);
  fprintf(stderr, "get %d %d %d\n", ret_get, (int) rlim.rlim_max,
      (int) bigger_stack);
  if (ret_get == 0) {
    if (rlim.rlim_cur < bigger_stack) {
      rlim.rlim_cur = bigger_stack;
      ret_set = setrlimit(RLIMIT_STACK, &rlim);
      if (ret_set != 0) {
    fprintf(stderr, "getrlimit %d, setrlimit %d\n", ret_get, ret_set);
      }
    }
  }



  printf("Enter the size of array:");
  scanf("%d", &n);
  a = malloc(sizeof(int) * n);

  for (i = 0; i < n; i++)
    //a[i]=rand()%100000;   //average case (random data)
    a[i] = i;           //ascending sorted input
  //a[i]=n-1-i;           //descending sorted input
  /*
   * printf("The UNsorted array is:\n");
   * for(i=0;i<n;i++)
   * printf("%d ",a[i]);
   * printf("\n\n");
   */

  start = clock();
  QuickSort(a, 0, n - 1);
  stop = clock();
  printf("Sorting time %f seconds\n", (double) (stop - start) / CLOCKS_PER_SEC);
  /*
   * printf("\nThe sorted array is:\n");
   * for(i=0;i<n;i++)
   * printf("%d ",a[i]);
   * printf("\n");
   */
  free(a);
  return 0;
}