4

In Microchip TCP/IP stack we encounter the following code:

    while(1)
{
    AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2<<8ul | MY_DEFAULT_IP_ADDR_BYTE3<<16ul | MY_DEFAULT_IP_ADDR_BYTE4<<24ul;
    AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
    AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2<<8ul | MY_DEFAULT_MASK_BYTE3<<16ul | MY_DEFAULT_MASK_BYTE4<<24ul;
    AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
    AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2<<8ul | MY_DEFAULT_GATE_BYTE3<<16ul | MY_DEFAULT_GATE_BYTE4<<24ul;
    AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2<<8ul  | MY_DEFAULT_PRIMARY_DNS_BYTE3<<16ul  | MY_DEFAULT_PRIMARY_DNS_BYTE4<<24ul;
    AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2<<8ul  | MY_DEFAULT_SECONDARY_DNS_BYTE3<<16ul  | MY_DEFAULT_SECONDARY_DNS_BYTE4<<24ul;
    // Load the default NetBIOS Host Name
    memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16);
    FormatNetBIOSName(AppConfig.NetBIOSName);

    break;
}

What's the function of the while(1)...break since it only executes one time ?

Victor
  • 41
  • 1
  • Not much to say from the code you posted, maybe is just a way to place the code within its own scope? – K-ballo Sep 20 '11 at 19:47
  • 1
    @K-ballo: which is weird, because plain `{ }` would also introduce a new scope, and there aren't any new locals defined in that block anyway. Who knows... – Greg Hewgill Sep 20 '11 at 19:49
  • @Greg Hewgill: Yes, I should have mentioned that. I assumed an old/crippled C implementation since its PIC oriented, you never know... – K-ballo Sep 20 '11 at 19:51
  • 4
    I would expect it was written by a really good hardware engineer (read: not a good software engineer). – mah Sep 20 '11 at 19:52

5 Answers5

4

Looks like legacy code to me. It's common to have a while(1) loop to initialize PLLs and such, but generally the breaking condition is dependent upon a register status bit in those circumstances.

If it were me, I would comment out the while(1) line, recompile, and see if any smoke appears ;-)

Throwback1986
  • 5,887
  • 1
  • 30
  • 22
1

Since there appears to be no continue statements or unconditional goto's in the loop body, I would say that it is just a way of enclosing a scope around that section of code. Interestingly, there are no automatic variables declared inside of the scope making the scope pretty useless.

tyree731
  • 453
  • 3
  • 8
0

Must be for scope. I don't see any other reason.

EDIT

Also, it could be someone was copy/pasting code from somewhere else that had a continue/break/etc in the block then just put their own code in there without thinking about it.

TWA
  • 12,756
  • 13
  • 56
  • 92
0

It is perhaps more usually written as a do { ... } while (0); loop which does not need a break in it (unless you need an early exit from the loop). In this context, as others said, it does not seem to provide any benefit unless one of the two functions is actually a macro, but the macro should be self-contained anyway.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

There's only one way to a truly definite answer: Ask the vendor for the history of that file (preferably with check-in log messages). Everything else seems like speculation.

Jens
  • 69,818
  • 15
  • 125
  • 179