I usually use an input file when I write programs so that I save myself from the hassle of entering digits again and again.
Here is a program which I wrote for quicksort which some where is giving me segmentation fault
#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);
}
}
and here is my input file input.txt
43 12 90 3 49 108 65 21 9 8 0 71 66 81
when I I compile it as follows
cc quicksort.c
./a.out < input.txt
Now the output I am getting is
Entering numbers in array
the sorted array is
Segmentation fault
What I want to know is I use gdb frequently to debug such problems. Is it possible that in gdb I take the input from the same file input.txt
my set of commands to use gdb is
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
Now what I want to know is how do I use the input file in gdb so that I do not enter again and again the array which I want to enter?