errors in gss
hi all
i done a program in c (language ) the program is quick sort when i compiled i did not get any errors but when run the program
if the data is more than one digit and in increasing order it gives segmentation fault i.e for example if the numbers 9,7,3,2,1,for this it gives the output 1,2,3,7,9 if the numbers are 12,11,10 then it gives segmentation fault i would like to know what is the reason and how would i remove it
Question information
- Language:
- English Edit question
- Status:
- Solved
- Assignee:
- No assignee Edit question
- Solved by:
- mycae
- Solved:
- Last query:
- Last reply:
Revision history for this message
|
#1 |
Have some outputs to stdout during execution so you can see where it dies
Revision history for this message
|
#2 |
ramu@ramki:~$ ./a.out
enter n value
3
12
15
9
9 12 15 ramu@ramki:~$ ./a.out
enter n value
3
13
12
11
Segmentation fault
ramu@ramki:~$
here n means how many numbers
On Tue, Mar 1, 2011 at 10:00 PM, actionparsnip <
<email address hidden>> wrote:
> Your question #147386 on Ubuntu changed:
> https:/
>
> actionparsnip posted a new comment:
> Have some outputs to stdout during execution so you can see where it
> dies
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#3 |
what is the information do you want ?
On Tue, Mar 1, 2011 at 10:06 PM, prasad.ram <
<email address hidden>> wrote:
> Your question #147386 on Ubuntu changed:
> https:/
>
> You gave more information on the question:
> ramu@ramki:~$ ./a.out
> enter n value
> 3
> 12
> 15
> 9
> 9 12 15 ramu@ramki:~$ ./a.out
> enter n value
> 3
> 13
> 12
> 11
> Segmentation fault
> ramu@ramki:~$
>
> here n means how many numbers
>
> On Tue, Mar 1, 2011 at 10:00 PM, actionparsnip <
> <email address hidden>> wrote:
>
> > Your question #147386 on Ubuntu changed:
> > https:/
> >
> > actionparsnip posted a new comment:
> > Have some outputs to stdout during execution so you can see where it
> > dies
> >
> > --
> > You received this question notification because you are a direct
> > subscriber of the question.
> >
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#4 |
Use a debugger, it will tell you where it dies. GDB is good for this, and so is valgrind. if you pass the "-g" option during compilation, then things like function names and variables can be retained, so you can be told exactly where the program goes wrong.
Both are in the repositories
GDB:
https:/
http://
Valgrind:
https:/
Revision history for this message
|
#5 |
1 #include<stdio.h>
2 int n,p,b,e,i,j,t;
3 long int a[10];
4 void quicksort(int ,int );
5 void main()
6 {
7 printf("enter n value\n");
8 scanf("%d",&n);
9 for(i=0;i<n;i++)
10 scanf("%ld",&a[i]);
11 quicksort(0,n-1);
12 for(i=0;i<n;i++)
13 printf("%ld ",a[i]);
14 }
15
16
17 void quicksort( b, e)
18 {
19 p=b;j=e;i=b+1;
20 while(i<=j)
21 {
22 while(a[i]<a[p] )
23 {
24 i++;
25 }
26 while(a[j]>a[p] )
27 {
28 j--;
29 }
30 if(i<j)
31 {
32 t=a[i];
33 a[i]=a[j];
34 a[j]=t;
35 }
36 }
37 t=a[p];
38 a[p]=a[j];
39 a[j]=t;
40 if(b<(j-1))
41 quicksort(b,j-1);
42 if((j+1)<e)
43 quicksort(j+1,e);
44 }
45
sir i am sending my program iand here p means pivot element b is for biging
and e is for ending and i send the debugging report also
ramu@ramki:~$ gdb ./a.out
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://
>
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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://
Reading symbols from /home/ramu/
(gdb) layout asm
ramu@ramki:~$ clear
ramu@ramki:~$ gdb ./a.out
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://
>
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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://
Reading symbols from /home/ramu/
(gdb) run
Starting program: /home/ramu/a.out
enter n value
2
11
10
Program received signal SIGSEGV, Segmentation fault.
0x0804854f in quicksort (b=0, e=1) at qs.c:22
22 while(a[i]<a[p] )
(gdb)
and i checked but i did not fingd ant thing becasue if i am sending single
digits it gives correct output so what is the reason
On Tue, Mar 1, 2011 at 10:53 PM, mycae <<email address hidden>
> wrote:
> Your question #147386 on Ubuntu changed:
> https:/
>
> Status: Open => Answered
>
> mycae proposed the following answer:
> Use a debugger, it will tell you where it dies. GDB is good for this,
> and so is valgrind. if you pass the "-g" option during compilation, then
> things like function names and variables can be retained, so you can be
> told exactly where the program goes wrong.
>
> Both are in the repositories
>
> GDB:
> https:/
> http://
>
> Valgrind:
> https:/
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https:/
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https:/
>
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#6 |
you are overflowing your buffer. i or p is larger than your buffer.
This is programming -- you need to be able to understand why this is happening
Revision history for this message
|
#7 |
As a hint, try using the "display" or "print" commands in GDB
Revision history for this message
|
#8 |
ok sir
and i cant understand gdb debugging for this program what is the solution ?
On Wed, Mar 2, 2011 at 1:54 AM, mycae
<email address hidden>wrote:
> Your question #147386 on gcc-defaults in ubuntu changed:
> https:/
>
> mycae proposed the following answer:
> As a hint, try using the "display" or "print" commands in GDB
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
>
> https:/
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https:/
>
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#9 |
to print variables type (in gdb)
print VARIABLE
so, for example. try (after your program crashes)
print i
and
print j
and you will see that one of these will be bigger than 9, which is the maximum index of your array.
if you want all of the local variables, try
info locals
if you want to step through your program, and print things at any stage, before you run the program type
break quicksort
then run the program with "run"
then the program will stop before running the quicksort.
then type
display i
and
display j
then type
step
then keep pressing enter to repeat the "step". Each step will execute one line.
Use this process to understand why your program crashes.
Revision history for this message
|
#10 |
sir thanks for your nice explanation ,i understand almost but i have some
doubts how can i change my array size at run i.e at debugging is there any
possibility and you said a command i.e
break quicksort
in my program quicksort is used in main i.e at line number 11 and in
function code two times they are at line numbers 41 and 43 my doubt is in
these three where break point is assigned please tell me for reference of
line numbers i send my program with this
1 #include<stdio.h>
2 int n,p,b,e,i,j,t;
3 long int a[10];
4 void quicksort(int ,int );
5 void main()
6 {
7 printf("enter n value\n");
8 scanf("%d",&n);
9 for(i=0;i<n;i++)
10 scanf("%ld",&a[i]);
11 quicksort(0,n-1);
12 for(i=0;i<n;i++)
13 printf("%ld ",a[i]);
14 }
15
16
17 void quicksort( b, e)
18 {
19 p=b;j=e;i=b+1;
20 while(i<=j)
21 {
22 while(a[i]<a[p] )
23 {
24 i++;
25 }
26 while(a[j]>a[p] )
27 {
28 j--;
29 }
30 if(i<j)
31 {
32 t=a[i];
33 a[i]=a[j];
34 a[j]=t;
35 }
36 }
37 t=a[p];
38 a[p]=a[j];
39 a[j]=t;
40 if(b<(j-1))
41 quicksort(b,j-1);
42 if((j+1)<e)
43 quicksort(j+1,e);
44 }
45
On Thu, Mar 3, 2011 at 4:20 AM, mycae
<email address hidden>wrote:
> Your question #147386 on gcc-defaults in ubuntu changed:
> https:/
>
> Status: Open => Answered
>
> mycae proposed the following answer:
> to print variables type (in gdb)
>
> print VARIABLE
>
> so, for example. try (after your program crashes)
>
> print i
>
> and
>
> print j
>
> and you will see that one of these will be bigger than 9, which is the
> maximum index of your array.
>
> if you want all of the local variables, try
>
> info locals
>
> if you want to step through your program, and print things at any stage,
> before you run the program type
>
> break quicksort
>
> then run the program with "run"
>
> then the program will stop before running the quicksort.
>
> then type
>
> display i
>
> and
>
> display j
>
> then type
>
> step
>
> then keep pressing enter to repeat the "step". Each step will execute
> one line.
>
> Use this process to understand why your program crashes.
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
>
> https:/
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https:/
>
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#11 |
Your array is of a static size, it is declared to be constant according to your code.
when you
break quicksort
the break is inserted at the very beginning of the quicksort function -- i.e. line 18 in the above program. The program will stop every time the code gets to this point.
As your function is recursive, this breakpoint may be hit multiple times.
Revision history for this message
|
#12 |
is there any why to change the array size at raun time sorry for irritating
you and these is my last question please tell me
On Thu, Mar 3, 2011 at 9:30 PM, mycae
<email address hidden>wrote:
> Your question #147386 on gcc-defaults in ubuntu changed:
> https:/
>
> Status: Open => Answered
>
> mycae proposed the following answer:
> Your array is of a static size, it is declared to be constant according
> to your code.
>
> when you
>
> break quicksort
>
> the break is inserted at the very beginning of the quicksort function --
> i.e. line 18 in the above program. The program will stop every time the
> code gets to this point.
>
> As your function is recursive, this breakpoint may be hit multiple
> times.
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
>
> https:/
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https:/
>
> You received this question notification because you are a direct
> subscriber of the question.
>
Revision history for this message
|
#13 |
If you beleive you need to change the array size, then you need dynamic arrays, which involves pointers and the "malloc" and "free" functions. See your favourite C book for an extensive discussion on this.
however with the quicksort algorithm it should be possible to sort N (or fewer) items in an array of length N.
Revision history for this message
|
#14 |
thanks for giving valuable information