开发者

PIC C - What's the purpose of this?

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_DEF开发者_如何学JAVAAULT_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 ?


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 ;-)


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜