PDA

View Full Version : This results in a segementation fault -



dE_logics
November 9th, 2009, 10:03 AM
#include<stdio.h>
main()
{
char input[1000];
short from = 0;
short jockey = 0;
input[0] = 'a';
while(input[from] != '!')
{
if(from > 0)
from++;
input[from] = getchar();
if(from == 0)
from++;
}
input[from] = '\0';
from = 0;
while(input[jockey] != '\n' || input[jockey] != '\0')
jockey++;
jockey--;
}

But why?

Arndt
November 9th, 2009, 10:05 AM
#include<stdio.h>
main()
{
char input[1000];
short from;
short jockey = 0;
while(input[from] != '!')
{
if(from > 0)
from++;
input[from] = getchar();
if(from == 0)
from++;
}
input[from] = '\0';
from = 0;
while(input[jockey] != '\n' || input[jockey] != '\0')
jockey++;
jockey--;
}

But why?

You should initialize 'from' to something. 0 perhaps? As it is now, it's undefined. It could be 3425141.

dE_logics
November 9th, 2009, 10:30 AM
That did not work.

MadCow108
November 9th, 2009, 10:33 AM
also your input[0] is unitialized
make a getchar before the while do loop
or replace it with a do while loop

your loop condition is always true so (a char can't be both newline and null) so it runs infinitly (or until segfault)

and you're missing boundary checks so it may segfault in both loops because of wrong input

Arndt
November 9th, 2009, 10:33 AM
That did not work.

The program seems to count on there being a '!' in 'input'. How does it arrive there?

How is the problem formulated that you're trying to solve? Is it homework?

dE_logics
November 9th, 2009, 10:50 AM
also your input[0] is unitialized
make a getchar before the while do loop
or replace it with a do while loop

your loop condition is always true so (a char can't be both newline and null) so it runs infinitly (or until segfault)

and you're missing boundary checks so it may segfault in both loops because of wrong input

Initializing input[0] did not work...thanks for telling that.

No, I would not like to use do while (starting off with programing after a long time).

I'm assuming the input to have multiple lines...so there will be a \n but there will always be a '\0' I think -


input[from] = '\0';

dE_logics
November 9th, 2009, 10:53 AM
The program seems to count on there being a '!' in 'input'. How does it arrive there?

The use has to input a '!' It's a necessity.



How is the problem formulated that you're trying to solve? Is it homework?

No, I never do homework.

Actually there's an accumulation of source tarballs (I've switched to Gentoo) that's occupying lots of space...so I was wondering about removing the old version of every sources tarball.

This is a cornered part of the program which finds such duplicates.

Arndt
November 9th, 2009, 11:35 AM
The use has to input a '!' It's a necessity.





There's no trace of obtaining any user input in your program.

dE_logics
November 9th, 2009, 11:38 AM
There's no trace of obtaining any user input in your program.

What about getchar()?...it's in stdio.

Arndt
November 9th, 2009, 11:41 AM
What about getchar()?...it's in stdio.

Sorry, that's obviously user input. But I was focused on the loop test. The first time it is encountered, nothing has been read yet.

efexD
November 9th, 2009, 12:29 PM
int main would be start

MadCow108
November 9th, 2009, 12:54 PM
have you fixed your infinity loop?
that is your main problem and the reason for the segfault
(and input needs initializing)

dE_logics
November 9th, 2009, 01:05 PM
Sorry, that's obviously user input. But I was focused on the loop test. The first time it is encountered, nothing has been read yet.

hummm...I sorta didn't get that.


have you fixed your infinity loop?
that is your main problem and the reason for the segfault
(and input needs initializing)

But I do not know how it's forming an infinite loop, for e.g this is the input -



gentoo-sources-2.6.30-r4
gentoo-sources-2.6.30-r5!

Then how can it from an infinite loop?

There's a \n and a \0.

while(input[jockey] != '\n' || input[jockey] != '\0') will end if it encounters a \n or \0.

Arndt
November 9th, 2009, 01:39 PM
hummm...I sorta didn't get that.



But I do not know how it's forming an infinite loop, for e.g this is the input -



Then how can it from an infinite loop?

There's a \n and a \0.

while(input[jockey] != '\n' || input[jockey] != '\0') will end if it encounters a \n or \0.

No, it will continue if it encounters something that is either not \n or not \0. Think about it, it's not the same thing. Imagine that input[jockey] is \n and do every tiny step of the above in your head.

StunnerAlpha
November 9th, 2009, 03:06 PM
hummm...I sorta didn't get that.



But I do not know how it's forming an infinite loop, for e.g this is the input -



Then how can it from an infinite loop?

There's a \n and a \0.

while(input[jockey] != '\n' || input[jockey] != '\0') will end if it encounters a \n or \0.

This:


while(input[jockey] != '\n' || input[jockey] != '\0')

...needs to be:


while(input[jockey] != '\n' && input[jockey] != '\0')

Because the loop needs to keep looping while the item from the array is not equal to '\n' AND '\0'. It needs to not be equal to either of them, thus you would use &&(and) instead of ||(or).

dwhitney67
November 9th, 2009, 03:45 PM
Right, the && is needed.

Hopefully the OP will see the need to clean up the code:


#include<stdio.h>

int main()
{
char input[1000];
short from = 0;
short jockey = 0;

input[from++] = 'a';

while((from < sizeof(input) - 1) && (input[from - 1] != '!'))
{
input[from++] = getchar();
}

input[from] = '\0';

while(input[jockey] != '\n' && input[jockey] != '\0')
{
++jockey;
}
--jockey;

/* and then what??? */

return 0;
}

Arndt
November 9th, 2009, 03:58 PM
Right, the && is needed.

Hopefully the OP will see the need to clean up the code:


#include<stdio.h>

int main()
{
char input[1000];
short from = 0;
short jockey = 0;

input[from++] = 'a';

while((from < sizeof(input) - 1) && (input[from - 1] != '!'))
{
input[from++] = getchar();
}

input[from] = '\0';

while(input[jockey] != '\n' && input[jockey] != '\0')
{
++jockey;
}
--jockey;

/* and then what??? */

return 0;
}


I think he may be sitting and waiting until we're done repairing his program...

dE_logics
November 10th, 2009, 08:22 AM
Yes, I got it.

Thanks!