SSpoke
SSpoke

Reputation: 5836

C while loop only runs if statement true?

I was looking at some C code and I found this line.

if (temp==NULL)
    while (1) ;

From my understanding when you get into a infinite loop there is no getting out unless you break, how does this work? does it break when if statement is not NULL if so, what makes it keep checking the if statement over and over again?

For more information look for the realboy source code the file is gboy_lcd.c

Line 304
https://github.com/guilleiguaran/realboy/blob/ed30dee751c3f78964e71930a8f87d2074362b9b/gboy_lcd.c

It's a very stable and good gameboy emulator though for linux

Upvotes: 4

Views: 212

Answers (6)

user3482784
user3482784

Reputation: 11

I wrote this emulator and, although the version you are looking at is very old, I haven't got rid of this 'flaw'. Take a look over here: http://sourceforge.net/p/realboy/code/ci/master/tree/src/gboy_video.c

The offending function is now called 'vid_frame_update()'. Specifically, line 141. With the addition for Super Game Boy support, this 'flaw' is also replicated at line 102.

If you take a look at function at line 168 ('vid_start()'), you'll see that the possible (pointer) values for 'temp' are allocated once and for all (these are, x1 through x4 and sgb_1 through sgb_4), so the test for bad allocation should be done here. I remember, however, that I was having some strange behaviour and added the 'flaw' solely for debugging purposes.

I'm sorry for the inconvenience, but am glad this happened because I never though something trivial like this could be source of confusion (maybe I just never though someone would actually look at the code). Better be more careful next time, heh!

Thanks.

Upvotes: 1

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

This should be simpler to understand that once if succeeds it enters while loop, iterating in a contiguous loop,

if (temp==NULL){
  while (1) { }
}

Upvotes: 0

pm100
pm100

Reputation: 50110

basically somebody is halting the program. If temp is NULL then the program will go into an infinite loop on the while statement. I would expect to see a comment in the code saying why he is doing this.

This is very common in embedded / micro code (ie the code running your TV set, fridge, smoke detector,...) because there is no way to stop / crash / alert. The only thing you can do is loop. During development you can use a debugger to break into the code to see whats happening

PS - why the downvotes - this is a good question

Upvotes: 6

zstewart
zstewart

Reputation: 2177

Assuming no outside interrupts are sent to the process/thread, if the if block succeeds, the while loop will in fact run forever. You are correct: there is no getting out of that loop.

To actually determine if this is a useful behaviour would require a bit more context.

Upvotes: 1

user376507
user376507

Reputation: 2066

There is no going back to check the value of temp once the check has been done and you enter the while loop.

Upvotes: 2

Almo
Almo

Reputation: 15861

This code starts a while loop if temp is NULL.

The while loop evaluates the expression (1) over and over and does nothing each time until 1 != 1 which will never happen.

Written more clearly with Allman bracing shows it:

if (temp == NULL)
{
    while(1)
    {
    }
}

As pointed out in a comment, this version shows that the if is not part of the while.

Upvotes: 5

Related Questions