【发布时间】:2011-06-21 07:41:28
【问题描述】:
我在编写程序时通常使用输入文件,这样我就免于一次又一次地输入数字的麻烦。
这是我为快速排序编写的一个程序,它的某些地方给了我分段错误
#include<stdio.h>
int partition (int *,int,int);
void quicksort (int *,int,int);
int main()
{
int i,j,a[15],choice;
int length;
printf("Entering numbers in array \n");
for(i=0;i<=14;i++)
scanf("%d",&a[i]);
printf("the sorted array is\n");
length=sizeof(a);
quicksort(a,0,length-1);
for(i=0;i<=14;i++)
printf (" %d ",a[i]);
}
int partition(int *num,int p,int r)
{
int x,j,i,temp;
x=num[r];
i=-1;
for(j=0;j<=r-1;j++)
{
if(num[j]<=x)
{
i=i+1;
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
num[i+1]=num[r];
return i+1;
}
void quicksort (int *num,int p,int r)
{
int q;
if (p<r)
{
q=partition(num,p,r);
quicksort(num,p,q-1);
quicksort(num,q+1,r);
}
}
这是我的输入文件 input.txt
43 12 90 3 49 108 65 21 9 8 0 71 66 81
当我编译如下
cc quicksort.c
./a.out < input.txt
现在我得到的输出是
Entering numbers in array
the sorted array is
Segmentation fault
我想知道的是我经常使用 gdb 来调试此类问题。 是否有可能在 gdb 中我从同一个文件 input.txt 中获取输入
我使用 gdb 的命令集是
cc -g quicksort.c
gdb
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file a.out
(gdb) break quicksort.c:3
(gdb) run
现在我想知道的是如何使用gdb中的输入文件,这样我就不会一次又一次地输入我想输入的数组?
【问题讨论】:
标签: c debugging gdb segmentation-fault