当前位置: 动力学知识库 > 问答 > 编程问答 >

c - how can I use input file in gdb

问题描述:

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?

网友答案:

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]))
网友答案:

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)

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: