- 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
Dude, I don't know about that. I think your stupid for begging the question that C++ is a superior language.
Admin
For those who say it's not a WTF, you are wrong. Sure it looks like a vtable, but how the hell do you maintain code where data is floating in a sea of NULLs?
Here's a maintainable implementation. I assume here that there are constants for what the offsets mean, but that's probably optimistic.
memset( vtable, 0, sizeof( vtable ) ); vtable[METHOD_START] = &start_diagnostic_session; vtable[METHOD_READ_ECU] = &read_ecu_identification; vtable[METHOD_READ_DATA] = &rd_data_by_common_identifier; vtable[METHOD_SECURITY] = &security_access; vtable[METHOD_WRITE_DATA] = &wr_data_by_common_identifier;
Admin
My old college teacher said C++ is only superior to C if you consider amount of documents produced by it.
Admin
Admin
That is exactly right.
If you're not part of problem, you end up being part of the solution provider....Martin Luthor King.
Admin
Nagesh, please stop posting... I still have a ringing in my ears from reading your posts. I think I'm going to write up something in Greasemonkey to visually block his posts from my screen.
Admin
Admin
Admin
I do.
Dear Internets,
Please post all of your stories about old teachers, bosses, and parents on this thread.
Sincerely, TDWTF HiveMind(tm)
Admin
That's very nice. But in small embedded systems -- which this presumably is -- we tend to favour storing things in program memory (i.e., flash or ROM) rather than in RAM. Firstly RAM's a precious resource (you might have, say, 512K of Flash and only 16K or RAM) so you don't want to waste it on non-varying things. And secondly you want to try and ensure that your code doesn't get corrupted (by cosmic rays or errant pointers elsewhere), and storing your lookup table in flash helps with this -- remember that your system may reset very rarely, perhaps only when the battery is disconnected.
Incidentally you don't need the ampersand. Some coding standards rule against it. Also, on many automotive systems the standard library (e.g., memset) is not available or its usage is discouraged. Oh yes, and null pointers are not guaranteed to be "all bits zero", so that's going to need a deviation against MISRA-C.
The original code isn't the tidiest I've ever seen, but it's fairly typical automotive code. The requirements, standards and development principles for this stuff are very different to what is state of the art for non-embedded applications software, but its no WTF.
Admin
Addendum (2011-02-23 17:14): But I wholeheartedly agree and approve.
Admin
Admin
Someone has stolen my account here! The impostor has posting in my account and defiling the name of my namesake. I did not notice until now because I was inspire by today's code. I am implementing it in code I am writing for big client, they will be very happy with this. I have shared this code with all my colleagues here in Hyderabad, we all agree this is the best code written. I do not know why you post this code on your site, there is no WTF in it at all. Thanks, Daily WTF!
Admin
And you call us racist for saying outsourced code from India sucks?
Admin
A quibble, though. If size is the issue, and the vtable is not dynamic, why the hell would there be nulls?
Admin
Maybe it's something like choking the chicken but more linuxey way?
Admin
Admin
The free online book on how to do this: http://msdn.microsoft.com/en-us/vbasic/ms788236
Admin
Admin
[quote user="JamesCurran" Well, I'd agree with you -- if they filled the array. For five entries, they'd be far better off with a different method. How 'bout 5 if()s :
Way more clear as to what it's doing, and a helluva lot smaller that wasting 512 (or 1024, depending on function pointer size) bytes on the sparely populated array. [/quote]
What you (and a bunch of other here are forgetting is there is a world of difference between compile time constants [stored in ROM] and runtime (even startup) calculation [stored in RAM]
I recendly did an embedded system, 256KB of ROM [Mask, not prom,eerom,etc.] and 2K RAM [radiation hardened RAM is EXPENSIVE].
There was one class which required about 40 bytes per instance, and had about 500 possible states (valid combinations of the members). Instead of using a conventional implmentation of fields in the clas holding the values, we stored ALL of the possible states in ROM [20KB], and each instance contained a single pointer to the appropriate state. When a calculation was performed on an instance, a "full" version was reconstituted in RAM, then calculations performed, and a search (optimized) of all of the possible values performed to find the appropriate match, we then retained just a "freeze dried" version.
Admin
In that case the real developer might have introduced a bug. It is not safe to memset pointers because a null pointer don't have to be numeric 0(All bits 0).
Admin
Or I could be wrong, and they used a random number generator to decide where to put their base class members in the vtable.
Admin
Admin
Admin
Dude, you just added like, 20 to 30 bytes to the size of the .text section with those stupid assignment instructions. And you caused the vtable to now have to exist in writable memory, which is at a premium on most embedded systems, instead of living on a cheap ROM. You fail.
Ever read that paper, "Incompetent and Unaware of it?"
Admin
Based on the code, I see no evidence that it is a vtable (i.e. a methodology for dispatching calls for an overridable method in a class). In fact, there is no evidence of any object oriented elements. It is a simple function dispatch table.
Looking back at the comments, it is quite likely that "vtable' was brought of more as an analogy than as a proper usage of the term.
Admin
This is clearly (at least to me) a KWP2000 (Keyword Protocol) dispatcher from an automotive ECU.
My company usually buys this part of the system from a third party supplier either in the form of source code or a precompiled library, which comes along with a code generator which generates ROM tables like this. This specific table is used to map the SIDs or service IDs (a standard term from the relevant ISO standard) which come in the fist byte of the request message to the function that handles that specific service. Normally this part of the code is generated automatically and is part of the third party module, and it is calling functions in the device specific code (written by us).
The benefit of writing the SIDs table like this is obviously faster lookup at the expense of a bit of extra ROM being used. Different ECUs would have more services defined so the table would be less sparse. Fortunately the third party solutions that we use are a little smarter and have two columns, one for the service ID and the other for the function pointer!
Admin
Admin
I don't see a WTF here. This is a lookup table to choose functionality based on an opcode. They initialize everything they know at compile time with the real values and everything else cleanly with null. Of course you could write that in a loop, but why waste precious runtime if you can do it at compile time? Computers weren't always as fast as they are now.
Admin
Umm... Where am I? Oh, not so much WTFs in articles as in the comments...
Admin
I have NULLed your array. Pray I don't NULL it further.
Admin
I also don't see the WTF. A function pointer dispatch table is anyday better than a giant fugly switch(){ case : } or a huge if () else if(). Not to mention better maintainablity and way way way more faster than those comparisions can ever dream of becoming (think pipelining on ARMs). Plus I doubt that somebody wrote that by hand, it probably was generated using a tool (or Excel, which is excellent for maintaining such arrays and readability). I guess that the author has not found the tool (or the excel sheet) to do so and is bitching about it.
Admin
It really is like I'm looking at the Win32 API!
Admin
Sing along... "99 NULLs in the method call, 99 NULLs in the call. You take one down - pass it around, 98 NULLs in the method call..."
Admin
Addendum (2011-02-24 08:35):
Well, it may seem so, however if you're developing for an embedded environment, without a file system, then it may well be considered best practice.Admin
Imagine that. Some hotshot knowitall developer submitted something that extends beyond his limited firsthand experience as a WTF.
Admin
[quote user="funluvncriminalDude, considering the function prototype starts with the word static (aren't all functions in C static, variables are a different story, but functions...), I don't think he's working with a vanilla C compiler, and this thing is really as misguided as it looks.[/quote]
You need to go learn C.... elements at File Scope can be either extern (visible to other compile units) or static (available only within the compilation unit). In this regard there is no difference between C and C++.
Admin
Why you force client to buy new server? OUr client still running our super fast quick code on 486 and Pentium machine. He never complain. Truly good code is one that runs on less resources.
Admin
Admin
Admin
What casinos? Gambling is illegal in India.
True story from Mahabharat. Look up Yudishthira, Draupadi, Gambling, Dice, Shakuni, Pandav.Admin
Definitely KWP2000. I don't see what's really that wrong with the code - maybe it could have been initialised a little more clearly but it's obvious to anyone who's been in the vehicle/automotive/machine control industry for more than 5 minutes what it is and what it does.
Admin
You have failed! Why make so much noise about writing some script?
You limp wristed, lack of moral fiber, noise making hypocrite cannot ignore me. That's true WTF.
We are legion... There I said it.. hoodarino.Admin
Forgot to mention: not necessarily precisely ISO 14230 Keyword Protocol 2000 but possibly a harmonised spec such as Swedish Implementation Standard (SSF) 14230. Either way, it's the same thing in different clothes.
Admin
Admin
Admin
You're East Indian. I'm West Indian (part Cherokee). Both are swarthy heathens, but your kind didn't chop the scalps off of the invading British horde for trophies. You also probably didn't eat entire British colonies, which is my educated guess as to what happened to Jamestown.
I bet they were delicious.
Admin
This code is obviously going into a million+ units of embedded car ECUs, so they can't just shit out fifteen minutes worth of Python code and tell the customer to spend more on hardware.
This just in: sometimes optimization is actually necessary!
Besides, it's just a sparse array of function pointers forming a dispatch table. I'd consider some common C++ "idioms" far less maintainable.
Admin
In that he should have just used the term "Red Indian". That's we Indians usually understand very quickly. Thanks for the clarification, though.
Columbus sailed the ocean blue, (forget the rest)
Admin