分割故障回路C99 [英] Segmentation Fault Loop C99
问题描述
这code是一个更大的计划的一部分。有一个主
函数code内运行良好,对不起,如果它的混乱。在第二,而
循环功能 Quick_mode_time
我得到一个分段错误,并且我不明白我怎么搞乱阵列到导致此。如果有人能在正确的方向指向我,我会非常高兴!
的#include<&stdio.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdbool.h GT;#定义timing_start()开始=时钟();
#定义timing_end(F)=毫秒(双)(时钟() - 启动)* 1000000.0 / CLOCKS_PER_SEC; \\
的printf(调用#F带%10.2f微秒\\ n!,毫秒);无效Quick_mode_time()
{
函数srand(时间(NULL));
INT年底= 500,最终= 5000;
诠释一个[结束],开始= 0;
双T1,T2,t_tot;
的printf(***************比较*************** \\ n); 的printf(快速排序冒泡(微秒)\\ n);
而(完!=最终){ 同时(开始与LT端)
{
一个[开始] =兰特()%结束; //< ---这将导致分段错误
开始++;
}
的printf(N =%d个,结束); T1 =时钟();
quicksort_time(一,0,结束); T2 =时钟();
t_tot =(双)(T2-T1)* 1000 / CLOCKS_PER_SEC;
的printf(\\ t%7.3f \\ t的,t_tot);
结束+ = 500;
}
}无效quicksort_time(INT X [],诠释第一,诠释去年){
INT支点,J,温度,I;
//快速排序algorithim低于!
如果(第一<上){
支点=第一;
I =第一;
J =最后; 而(I< j)条{
而(X [1] - ; = X [枢轴]&放大器;&放大器; I&下;最后)
我++;
而(X [J]> X [透视])
j--;
如果(ⅰ&所述; j)条{
TEMP = X [I];
X [i] = X [J]。
X [J] =温度;
}
} TEMP = X [透视]
X [透视] = X [J]。
X [J] =温度;
quicksort_time(X中,首先,J-1);
quicksort_time(t,J + 1,最后一个);
}
}
这行:
int类型的[结束]
创建了500个int(最终被设置为500之前的线)
数组这行
A [开始] =兰特()%(结束+ 1月底)+ END;
写入 A [开始]
这是只要确定为开始
小于500。
这似乎是由刚刚超过这条线检查:
同时(开始与LT端)
但后来在code,你有:
结束+ = 500;
如此突然,年底可> 500,这意味着开始可> 500。这意味着你写你的数组边界之外。
一个解决将是改变你的声明,使 A
足够大:
int类型的[决赛]
This code is a part of a much larger program. There is a main
function within the code and it runs fine, sorry if its messy. Within the function Quick_mode_time
in the second while
loop I get a segmentation error, and I dont understand how I'm messing the array up to cause this. If someone could point me in the right direction I would be very happy!
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#define timing_start() start = clock();
#define timing_end(f) msec = (double)(clock()-start) * 1000000.0 /CLOCKS_PER_SEC; \
printf("Calling "#f " takes %10.2f microseconds!\n", msec);
void Quick_mode_time()
{
srand(time(NULL));
int end=500,final=5000;
int a[end],beginning=0;
double t1,t2,t_tot;
printf("***************COMPARISON***************\n");
printf(" quicksort bubblesort (in microseconds)\n");
while(end!=final){
while(beginning<end)
{
a[beginning]=rand()%end; // <--- This causes a segmentation fault
beginning++;
}
printf("N=%d",end);
t1=clock();
quicksort_time(a,0,end);
t2=clock();
t_tot= (double)(t2-t1)*1000/CLOCKS_PER_SEC;
printf("\t%7.3f\t", t_tot);
end+=500;
}
}
void quicksort_time(int x[],int first,int last){
int pivot,j,temp,i;
// The Quick Sorting algorithim is below!
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort_time(x,first,j-1);
quicksort_time(x,j+1,last);
}
}
This line:
int a[end]
creates an array of 500 ints (end is set to 500 in the line before)
This line
a[beginning]=rand()%(end+1-end)+end;
writes to a[beginning]
which is OK as long as beginning
is less than 500.
This seems to be checked by this line just above:
while(beginning<end)
but later on in your code, you have:
end+=500;
so suddenly, end can be > 500, which means beginning can be > 500. Which means you write outside your array boundaries.
One fix would be to change your declaration so that a
is big enough:
int a[final]
这篇关于分割故障回路C99的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!