1

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?

Registered User
  • 5,173
  • 16
  • 47
  • 73
  • Possible duplicate of [How to use gdb with input redirection?](http://stackoverflow.com/questions/4758175/how-to-use-gdb-with-input-redirection) – Bart Louwers Feb 10 '16 at 03:18

3 Answers3

4

As always google is your friend in such "common" use-cases.

How to load program reading stdin and taking parameters in gdb?

Input redirection in gdb (MinGW)

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
4

For debugging purposes, add this line to your program and comment out the first for-loop

int a[] = {43, 12, 90, 3, 49, 108, 65, 21, 9, 8, 0, 71, 66, 81};

To get maximum debug-information, compile it using

gcc -ggdb3 qs.c

and run it in gdb as

gdb -q ./a.out 

and never exit your gdb session! If you recompile it, gdb will automatically notice and reload the binary; in this way you will always have your breakpoints set and everything else you crafted.

Since others have given you a hint to where the problem is, I might reveal a common macro to get the size of an static allocated array :-)

#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • yes I got what that macro is ,just wanted to know if I do not fill all the 15 indices and only some numbers in array are filled then in that case will your macro sizeof(x) return the space occupied by only the filled places or all 15 places i.e. 60 bytes. – Registered User Jun 21 '11 at 08:14
  • If you statically allocate an array to contain 14 items as in `a[14]` then that is what you will get. It doesn't matter if you populate the array or not. It will contain non-initialized values, but still 14 of them. As always with gdb. Try it! In gdb you can call any macro/function, i.e. type `p sizeof(a)` and `p sizeof(a)/sizeof(a)[0]` and look at the output – Fredrik Pihl Jun 21 '11 at 08:22
  • great I got your point.I just made another simple program to see how your macro works,I did not populated an array to full but it gave me the size of full array.So this just gave rise to a question is there a way I can know how many places of an int array have been populated with numbers i.e. I do not want to know the length of full array just how many numbers have been entered. – Registered User Jun 21 '11 at 08:36
  • Nope, not really, not for static allays. You could allocate it dynamically using malloc/realloc. For static arrays you could populate it with a known value at startup and check for that, but the value might be added by user later on and thus destroying the count. – Fredrik Pihl Jun 21 '11 at 08:50
  • 1
    the question was about piping an input file into an execution – ealfonso Mar 29 '15 at 15:32
  • See http://stackoverflow.com/questions/4758175/how-to-use-gdb-with-input-redirection for an actual answer if this doesn't get closed. – Bart Louwers Feb 10 '16 at 03:18
0

The reason for the seg fault maybe this line.

length=sizeof(a);

Its 60.

Your r value max should be 15. An integer is allocated 4 bytes. So your call overflows,hence the error.

elricL
  • 1,188
  • 3
  • 11
  • 20
  • why is that line an error? I understand that integer is 4 bytes,o OO do you mean to say I should use strlen and not sizeof.. – Registered User Jun 21 '11 at 08:10
  • great point.Just wanted to know if we have a partially filled array then sizeof(a)/sizeof(a[0]) will return the size of only those members which are occupied or will return all 15 i.e. 60/4 – Registered User Jun 21 '11 at 08:16
  • @Registered User. You cant use strlen directly on a integer type. To get the size, I would suggest asking for size earlier and pass it to the function.I also don't understand what you mean by a partially filled array.How will you distinguish 'filled' data from 'unfilled' data? – elricL Jun 21 '11 at 09:16