Fun with static string arrays



  • Whoever wrote this was being clever. He knew that the descriptions would need more space than the names. There was only one small misunderstanding.

    static char *RANGECODE_NAME[32] =
    {
      "GPS L1 C/A",
      "WAAS L1 C/A"
    };

    // aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmm <= 128 chars
    static char *RANGECODE_DESC[128] =
    {
      "BPSK modulated PRN code. Chip rate of 1.023MHz and 1023 chip long (1 ms repetition). Datamodulation of 20 bps.",
      "BPSK modulated PRN code. Chip rate of 1.023MHz and 1023 chip long (1 ms repetition). Datamodulation of 200 bps."
    };



  • I'm not surprised... There are so many people that barely understand memory management / allocation that hardly anything like that surprises me anymore.



  • The real fun is that things like that actually work without problems (who cares about a few hundred bytes wasted), so other (unexperienced) people who read that code might think that this is correct and learn to do it like that.



  • I believe you have missed it entirely.

    The comment that drags on for so long is to give him a vertical reference to where 128 characters comes.  Note how, where his descriptions for his SS modulation schemes are located, the left-quote lines up with the space after his double-slash.  This makes it very readily visually apparent if one of his description strings exceeds 128 characters.

     



  • @Critter said:

    I believe you have missed it entirely.

    The comment that drags on for so long is to give him a vertical reference to where 128 characters comes.  Note how, where his descriptions for his SS modulation schemes are located, the left-quote lines up with the space after his double-slash.  This makes it very readily visually apparent if one of his description strings exceeds 128 characters.

     


    From your message text, I'm not able to discern whether you realize this, but I'll spell it out here anyway. =)

    The real WTF arises from the fact that static char *RANGECODE_DESC[128] does not declare an array of 128-byte strings, but rather declares a static array of 128 pointers to char of which only two are used here. (Since he is pointing to string literals, there is no real 128 character limit - he could point to the text of the US Constitution in there if he really wanted to, really.)


    A static char *RANGECODE_DESC[] = { ... } would serve him better, or possibly a static char RANGECODE_DESC[][128] = { .. } (if he reaaaly wanted 128 bytes each, but that's actually probably not necessary for a message table.)



  • @Critter said:

    I believe you have missed it entirely.

    The comment that drags on for so long is to give him a vertical reference to where 128 characters comes.  Note how, where his descriptions for his SS modulation schemes are located, the left-quote lines up with the space after his double-slash.  This makes it very readily visually apparent if one of his description strings exceeds 128 characters.

     

    But not, of course, in the variable-width font used to display this code here. That's The Real WTF®  here.



  • @Critter said:

    The comment that drags on for so long is to give him a vertical reference to where 128 characters comes.

    Yup, and that brings in mind a few interesting thoughts. Like "I guess they bought their editor from the expensive shop - both of my recycled editor and the editor built out of coffee machine parts support this fancy 'column number display' thing, and the people who buy expensive editors often get amazed when they hear about this thing".

    Or "Doesn't the compiler warn or raise error about too long/overlapping literal strings? Been a while I've touched C so I have no idea if compilers can do that. Rest assured they should."

    Or "Wonder what to do with multi-line comments... or can they even be done? These silly Mac things don't have a C compiler installed so I can't try it out."

    You know, things like that.



  • @fennec said:


    From your message text, I'm not able to discern whether you realize this, but I'll spell it out here anyway. =)

    The real WTF arises from the fact that static char *RANGECODE_DESC[128] does not declare an array of 128-byte strings, but rather declares a static array of 128 pointers to char of which only two are used here. (Since he is pointing to string literals, there is no real 128 character limit - he could point to the text of the US Constitution in there if he really wanted to, really.)


    A static char *RANGECODE_DESC[] = { ... } would serve him better, or possibly a static char RANGECODE_DESC[][128] = { .. } (if he reaaaly wanted 128 bytes each, but that's actually probably not necessary for a message table.)

    And let's not forget that a 128-byte C string can only store 127 characters.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.