- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Admin
Huh!!! I never heard that before...
Admin
Admin
Admin
Admin
Wow, a double post... Now that's a LOT of love.
Admin
Admin
I'll join the contingent that agrees this kind of system is perfectly acceptable and normal, especially in embedded systems.
Two changes I would make to make it less WTFy:
Typedef the function pointer type to make the code more readable. Function pointer semantics are a black art to a lot of people to begin with, so declaring an array of them without a typedef isn't being particularly kind to those who aren't C veterans.
Change SID[MAX_NUM_SERVICE_IDENTIFIERS] to just SID[], and add a static assert (compile-time assert) that the size of the SID array == MAX_NUM_SERVICE_IDENTIFIERS. Why? Because just putting MAX_NUM_SERVICE_IDENTIFIERS as the array size doesn't guarantee that you'll have initialized the correct number of values. Instead, it's a far better practice to let the compiler size the array based on what you've entered, and then have it assert that the size it arrived at matches your expected value. That way if either the array contents change or MAX_NUM_SERVICE_IDENTIFIERS changes it will be caught on the next build attempt.
For bonus points, I would replace the NULLs with a function that asserts in debug builds.
Dan.
Admin
Well,
It's not the most ideal way to do it but it isn't unheard of. It's for processing messages for automotive protocols. I'm willing to bet that start_diagnostic_session is at position 0x10 and security_access is at position 0x21.
The RWTF here is that you should understand the nature of the thing you are maintaining. I'm not saying a table full of NULL pointers is the best approach -- but I could see how it could be useful. Especially if there are multiple tables like this and the current state or mode of communication adjusts the table used.
Having worked on this kind of stuff I tend to use switch/cases for these since the messages you deal with are sparse.
Admin
Or my C99 version above, which will initialize exactly the fields which are meaningful to initialize, and set the rest to NULL. Trouble is the lack of C99 in the embedded C market.
(Does this thread belong on comp.lang.c instead?)
Admin
I doubt that's what happened. More likely, the submitter went poking through the codebase, found this thing, didn't immediately have a clue what it was for, and assumed it must therefore be a WTF.
What makes it doubly hilarious is that this code was most likely automatically generated, rendering any maintenance argument moot and making the suggestions here of how to make it more "maintainable" look completely ridiculous.
Admin
TRWTF is the lack of typedefs, useless use of ampersand and sparse array initialization.
Captcha: eros -- C is only for real men.
Admin
Seriously, I'm glad they're here.
Admin
Admin
Admin
Admin
All I can say is, "NULL, NULL, NULL. NULL, NULL, NULL and NULL."
Admin
I expect that if SID[id] is NULL the calling code sends a "service not supported" response.
Admin
I got it! TRWTF is Johnny D bitching about pretty standard scalable solution!
Admin
Handing over development/maintenance to an incompetent programmer not knowing standard C practices and not giving him information about the code generation is TRWTF.
Admin
Admin
I really liked this article and comments keep up the good work. Researching function pointers was a very educational way to spend a slow Friday afternoon at work.
Looks like he is declaring an array of pointers SID. Where the majority of the contents are NULL pointers and the non NULLs are the addresses of functions.
To me the WTF is the risk of de-referencing a NULL pointer.
Admin
I was about the post almost identical code, until I saw your post. The only change I would recommend is that the literal subscript values should probably be replaced with the enum values identifying the opcode number or message number (depending on what type of dispatch table this is) that will be used when actually indexing this array.
One should mention that your code is C99, and thus if for some reason you absolutely must use a C90 compiler, it would not work.
Admin
Well, aren't you just the coolest.
Admin
If you say so Mr. Hat.
Admin
Nagesh, you are very racist, biggotted, small minded, and xenophobic for referring to native americans as "Red Indian". BTW code from India still sucks.
Admin
The code is self-documenting.
You count NULLs on each line and convert the numerics to the equivalent ASC chars to get a short succinct comment.
Admin
It's just NULLs, all the way down.
Admin
Yes, actually, I really had no idea what it was doing. And neither did much anyone else in the company. Finally I found a guy, who told me how that structure works. But that still doesn't explain, why there are 255 cells being used? Wouldn't just five cells be enough? Or, like suggested in an earlier post; five if's or a switch-case?
Admin
Admin
I think this code looks reasonable... not great, but reasonable. I probably would have used a switch statement with an enum here, but I'm not an embedded programmer and I can see why this would be more reasonable in an embedded context.
However, if this code is machine-generated, then the input files for the code generator should have been the "real" source code, with the snippet being an incidental part of the build process. So TRWTF is how did Johnny D even see this code!
Admin
Admin
Java programmers.