我已經用C語言編寫了這段代碼,它適用於小輸入和我能想到的所有測試用例。但是當一個大文件作爲輸入時,它會給出SIGABRT錯誤。有人能解釋我的原因嗎?在C(gcc)代碼中獲取SIGABRT錯誤
#include<stdio.h>
#include<string.h>
void q_sort(char **numbers, int left, int right)
{
int l_hold, r_hold,temp;
char *pivot;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while (strcmp(numbers[right],pivot)>=0 && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while (strcmp(numbers[left],pivot)<0 && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
temp = left;
left = l_hold;
right = r_hold;
if (left < temp)
q_sort(numbers, left, temp-1);
if (right > temp)
q_sort(numbers, temp+1, right);
}
int main()
{
int x,y,i,j;
int *arr;
char **str;
int *count;
while(1)
{
scanf("%d%d",&x,&y);
if(x==0 && y==0)break;
str =(char **)malloc(sizeof(char *)*x);
count=(int*)malloc(sizeof(int)*x);
i=0;
while(i<x)
{
str[i]=(char *)malloc(sizeof(char)*y);
scanf("%s",str[i]);
i++;
}
//sizeof(str)/sizeof(*str)
q_sort(str,0,x-1);// sizeof(str)/sizeof(char *), sizeof(char *),cmp);
i=0;
j=0;
arr=(int *)malloc(sizeof(int)*x);
while(i<x)
{
arr[j]=1;
while(i<x-1 && strcmp(str[i],str[i+1])==0)
{
i++;
arr[j]+=1;
}
j++;
i++;
}
for(i=0;i<x;i++)
{
count[i]=0;
}
i=0;
while(i<j)
{
count[arr[i]-1]++;
i++;
}
for(i=0;i<x;i++)
{
printf("%d\n",count[i]);
}
free(count);
free(arr);
for(i=0;i<x;i++)
free(str[i]);
free(str);
}
return 0;
}
你試過調試器嗎? –
[SSCCE](http://sscce.org)意味着你提供了一些緊湊的東西。不要指望我們閱讀那些混亂,格式不正確的代碼。 – 2012-12-21 17:55:00
使用'gcc -Wall -g'編譯所有警告和調試信息,並用'gdb'和'valgrind'進行調試; 'scanf'的''%d%d''格式看起來真的很可疑(或者至少檢查一下'scanf'的結果) –