Time to ask?

A

Andrew Poelstra

ah, found it

You have a semicolon straight after the while statement in getops().
This will produce the hanging, becuase you will have an infinite loop.
Remove this and it should be fine. Also make sure that getops()
returns something or you will have another infinite loop in main()

1) Quote context.
2) Your last statement isn't necessarily true, because falling off of
a non-void function is undefined, and may very well terminate the loop
in main, overwrite system data, or draw demons from your nose.

Good catch, though. Perhaps I should have looked for typos. :)
 
A

Andrew Poelstra

Hi MQ,
I think that is correct, as it simply eliminates tabs and spaces. But,
I have found something that does not make sense.

The statement:

for ( i=0; i <= 10; i++)
ungetch('T');

does not increment "i" when it returns from ungetch. Not sure if this
has something to do with it.

How do you know that i is not incremented?
 
M

mdh

Andrew said:
How do you know that i is not incremented?
Well, on stepping through it, the first call to ungetch() does not
hang. When the code returns after ungetch, i is still "0". I would have
expected it to be "1". If I try the next step, it hangs.
 
A

Andrew Poelstra

Well, on stepping through it, the first call to ungetch() does not
hang. When the code returns after ungetch, i is still "0". I would have
expected it to be "1". If I try the next step, it hangs.

Here's your `for' loop, with your step commented in:

i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++;
goto topOfLoop;
}

Does your debugger hang or the code? The next step shouldn't
cause any problems.
 
M

mdh

Andrew said:
Here's your `for' loop, with your step commented in:

i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++;
goto topOfLoop;
}

Does your debugger hang or the code?


Yes!!! And the interesting thing is that when the code returns from
ungetch, it does not increment i either.

I think another poster mentioned that a return from a void function is
undefined. I do not know enough exactly to know the implication of
this, but I wonder if this has something to do with it?
 
A

Andrew Poelstra

Yes!!! And the interesting thing is that when the code returns from
ungetch, it does not increment i either.

Which one? It will increment i if you give it the chance.
I think another poster mentioned that a return from a void function is
undefined. I do not know enough exactly to know the implication of
this, but I wonder if this has something to do with it?

It's certainly possible. UB (undefined behavior) can cause anything
imaginable.
 
M

mdh

Andrew said:
Which one? It will increment i if you give it the chance.


Well, as you step through,<<<- 1 ,<<<- 2,<<<- 3 are successful.
Ungetch() executes the first time. The call is returned to <<--4, and
it hangs there. i is never incremented.
 
A

Andrew Poelstra

Well, as you step through,<<<- 1 ,<<<- 2,<<<- 3 are successful.
Ungetch() executes the first time. The call is returned to <<--4, and
it hangs there. i is never incremented.

Well, you need to fix all of the problems with your code that various
people have pointed out. That should eliminate the undefined behavior.
From there, post the corrected code if it still doesn't work.

Until you do that, it could be a bug in your debugger just as easily
as it could be your faulty code.
 
K

Keith Thompson

mdh said:
I think another poster mentioned that a return from a void function is
undefined. I do not know enough exactly to know the implication of
this, but I wonder if this has something to do with it?

No, a return from a void function isn't undefined.

A return statement that returns a value is illegal in a function
declared to return void. I presume that's not your problem; if it
were, your program wouldn't have compiled.

A return statement *without* an expression in a function that returns
a type other than void is legal in C90, but illegal in C99. It's a
bad idea in any case. At best, it's going to return garbage to the
caller, which will presumbably be expecting something sensible.

Falling off the end of a function (i.e., reaching the closing "}"
without executing a return) is ok if the function returns void, and a
bad idea otherwise. For a non-void function, this will also return a
garbage value to the caller; the caller's attempt to use the result
invokes undefined behavior.

I wasn't able to find the code in question, so I don't know whether
any of this applies to your program. If it does, fix it; there's not
much point in doing anything else until you've fixed any known bugs.
(That's not *always* true; there are times when you can be sure that a
given bug isn't the cause of the misbehavior you're seeing. But such
assumptions can easily be wrong.)
 
R

Richard Heathfield

mdh said:
I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.

It doesn't actually hang on my system, and neither can I see any immediate
reason why it would. It does block for input, but as long as I keep it fed
it continues to execute quite happily.

It just doesn't do anything.

I think you're expecting getops() to do something cool, but at present it
doesn't have any code in it other than "skip white space" and "print two
numbers".

What were you expecting to happen?
 
M

mdh

Keith said:
any of this applies to your program. If it does, fix it; there's not
much point in doing anything else until you've fixed any known bugs.

I think all bugs are now fixed...but i still get the same issue. I have
stripped down the project to this: It now compiles and runs and prints
the expected output.

viz:
Printing line 1
Printing line 2
Printing line 3
Printing line 4
Printing line 5
Printing line 6
Printing line 7
Printing line 8
Printing line 9
Printing line 10
Printing line 11

but it hangs if I am stepping through it (Where indicated). Perhaps
some input from Xcode is warranted? ( I have posted there, but not too
much help yet)

# include <stdio.h>
# define MAXNUM 10


int getops(char s[]);

int main (){

char s[MAXNUM], type;


while ( (type=getops(s)) != EOF){

switch (type) {

case 'T':
printf("%s\n", s);
break;
}

}

return 0;
}


/*****GETOPS******/
void ungetch(int c);
int getops(char s[]){
int i;

/**while ( (c = getch()) == ' ' || c =='\t'); **/

/*for ( i=0; i <= 10; i++)
ungetch('T');*/


i = 0;
topOfLoop:
if (i <= 10)
{
ungetch ('T');
/* i is still 0. */
i++; /** Hangs here after return from ungetch in debugger**/
goto topOfLoop;
}

return EOF;

}

/***** GETCH & UNGETCH*****/
int j=1;
void ungetch(int c){
printf("Printing line %d\n", j++);

}
 
R

Richard Heathfield

MQ said:
Sorry Andrew, is this because some newreaders cannot view previous
messages?

It's unwise to assume otherwise. It's also unwise to assume that everyone's
newsreader is capable of correctly assembling threads. It's also unwise to
assume everybody gets their newsfeeds in the same order that you do. It's
also unwise to assume that a previous message is retained on all servers
for the same length of time it's retained on yours. For all these reasons
and more, quote sufficient context to remind people of the particular
subject you are talking about.
 
M

mdh

Richard said:
What were you expecting to happen?


Richard, I started off trying to do something else. Got side-tracked by
the "debugging" bug. Still getting it.(See my answer to Keith Thompson
above). The reason I am pursuing this is that it has happened before,
where it compiles and runs, but then in the debugger it hangs.
 
K

Keith Thompson

MQ said:
I believe I did

Without knowing who wrote "Correct me when I'm wrong", or what it was
about, the above makes very little sense.

Please don't snip attribution lines (the "So-and-so writes:" lines),
and please leave in enough context so your followup makes sense on its
own.
 
R

Richard Heathfield

mdh said:
Richard, I started off trying to do something else. Got side-tracked by
the "debugging" bug. Still getting it.(See my answer to Keith Thompson
above). The reason I am pursuing this is that it has happened before,
where it compiles and runs, but then in the debugger it hangs.

Your program periodically requires input. How are you supplying this input?

The debugger may /appear/ to hang when your program is expecting input. Find
out how to supply input during the program run.
 
M

MQ

It's unwise to assume otherwise. It's also unwise to assume that
everyone's

ok thanks, my bad...
 
M

MQ

Keith said:
Without knowing who wrote "Correct me when I'm wrong", or what it was
about, the above makes very little sense.

Please don't snip attribution lines (the "So-and-so writes:" lines),
and please leave in enough context so your followup makes sense on its
own.

The poster responsible will know...but thanks, I will in future (and in
present as you can see :)
 
R

Richard Heathfield

MQ said:
The poster responsible will know...

Not necessarily. Between writing his article and reading your reply, the
poster may have dealt with dozens or even hundreds of other articles on a
number of subjects in various newsgroups. Why should he remember one minor
discussion among many, purely on the basis of five or six words?

Watch to see how others quote context. Most people get it right, pretty
much. Emulate them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top