- 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
Agreed. The submitter must be a Java programmer. There's nothing wrong with this. I know of enough places where it's used (usually the last var is a zero-length array, in C99 IIRC you can declare this by creating an array without specifying a length at all (ie char *something[])).
Admin
Yes, maybe guilty of posting after a few beers, point is still valid though. More quality less quantity please.
One of the front page articles has mentioned that Alex thinks this is taking too much of his time, so he should not be under such pressure to release articles.
Admin
What does it do? Simple. It first approximates (to about the correct order of magnitude) what the solution is using that amazing hack (which reinterprets the float bits as an int, subtracts from a magic number, and reinterprets back as a float); it's very tricky and depends on exactly how IEEE floats are arranged, but it does work (the float is a bit off from something that would just manipulate the mantissa because that seems to give better results for reasons that nobody seems to know!) Then, it does a quick round of Newton-Raphson to get the mantissa about right; luckily, this is a function for which it converges quickly, so one round does a creditable job. If you wanted scientific accuracy, you'd do a few more rounds of N-R, but it converges so fast that you don't have to do many, and as this is presumably part of some code that is critically dependant on being fast (graphics engine or physics engine) speed is better than accuracy.
I only wish I was good enough at IEEE math to be able to say that I worked this out myself; I had help (from someone who was very impressed by the hackiness).
Admin
Admin
Actually, if you have tens of millions of structures of the same size, it can be worthwhile writing some very fancy custom allocator code to pack them in as tight as possible; not very easy to make portable, but the saving is colossal...
In any case, depending on your PoV and the competence of the code's original author, WTFs in memory management code are either rare or frequent. Use of malloc() in production in performance-critical threaded code though, that's a prime example and most C programmers don't know it. (It's the same with C++, except there the way of dealing with it - defining a new allocator core for new IIRC, though I don't remember how - is different.)
Admin
Admin
Damn I thought i knew C quite good till this example. I've used tons of this struct but I never asked myself how these were defined...
Definetly not a WTF in C but a WTF in C++ (there you should use std::vector<>)...
Good Job to have this post, Cheers, Ema! :-)
Admin
Actually the code isn't correct ISO-C.
i = * ( long * ) &y;
Casting around pointers like this and then referencing them violates type aliasing and requires options such as -fno-strict-aliasing in gcc. With the default options you risk miscompilation! Newer gccs would also warn.
These options typically tends to generate much worse code because the optimizer cannot make many assumptions.
The only standard blessed way to reference a variable with a different type is to use it as char * (with memcpy() etc.)
Admin
I like the CSOD, I just wish the quality was higher. It would also be cool if there was a CleverCode section as well, where things that look like WTFs but are not could be posted.
Admin
Bye bye now. Take your time and don't hurry back.
You don't have to read any article at all. Just don't come here. Go to /. or wherever you like to hang out.
If you do come here, STFU with all the complaining and whining about the name and the content. I can't speak for everyone else, but I'm sure that others besides myself are tired of hearing from you crybabies.
How about a section called "Loser's List"? We could make you the first entry.
Admin
By 'the sizes of the types' I was referring to sizeof(OrdArith::OrdElem) and sizeof(p), not the length of the myOrd array, and how to get the length of the array from these sizes and the data in the struct. Since I stated this length was dependent on the values in the structure at runtime, I agree that the it isn't known at compile time, that using sizeof will not return the array length, and that the programmer has to take care of the size himself, probably using something like the method above. Sorry that wasn't clear in your reading of my post; there's a limit to how much detail it's worth putting into a comment on a humorous forum.
Admin
That's why most modern CPUs use their own software, called microcode.
Admin
I'm not sure that this construct and std::vector<> solve the same problem. There are certainly more "clean" ways to handle variable-length lists as members of a structure, even in C. The reason for this particular way of doing it is to make sure that the variable length data is contiguous with the header structure. There are a number of reasons for doing this, from concerns about memory fragmentation to convenience when dealing with input data. Using an std::vector<> doesn't do that.
Admin
Haha I love it, KenW-ischenko was rude to me
Briliant
Sincerely,
UnresolvedExternal
Admin
Yeah I agree concerning the C part of "the variable length data is contiguous with the header structure". But in C++ the type used could not be a primitive type at all, so you have to use appropriate constructors (like new) and/or portable containers as std::vector<> . You miss that data is contiguous but hey this is C++ (C++ struct/classes/types aren't just POD as in C).
Cheers, Emanuele.
Admin
Actually there is; if the class is a "Plain Old Data" struct then it basically has to behave like a C struct, including that the members must occur in the order given and there is no 'hidden data'. Informally, a POD-struct is a struct (or class) that looks like a C one! In this case the public: modifier makes no difference. This struct would be POD if and only iff that OrdElem thing is also a POD (e.g. a built-in type, or another simple struct).
Admin
This does not comply with the C++ standard. It's a safe bet that the GNU C++ compiler implements extensions to support this syntax and semantics.
Admin
I'd argue that it's still a legitimate way to do things if you know you are dealing with basic types or structs of basic types. If you're using C++, you probably have very little hope of getting fine-grained control over heap fragmentation, but imposing structure on binary blobs (binary files, network packets, etc.) is still useful and quick with a method like this.
Maciej
Admin
Sometimes this makes sense. I doubt this is a good idea in C++ though. But the real WTF is to declare a struct explicit as public as structs are public by default.
Admin
This isn't a WTF, in fact it's so standard the very operating system you're most likely reading this on is using it. Excerpt from the Win32 API:
typedef struct tagBITMAPINFO { BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors[1]; } BITMAPINFO, *PBITMAPINFO;
http://msdn.microsoft.com/en-us/library/dd183375(v=VS.85).aspx
Admin
Maybe it was standard 20 o 30 years ago. And surely it appears in linux kernels. It wouldn't be a problem if there weren't other possibilities to have a variable sized list of things to sum over. But, my friend... It's not portable. It's not safe. At the very least change made not only in the structure, but in its environment, all the thing will crash. And, ey, linux kernel crashes are not such a rare thing. How about a linked list? How an explicit memory buffer to ask memory chunks? How a little bit of design? The real WTF is the fact that so much people don't ever recognize this as a complete failure of programming and engineering even after presented as such, with annotations. At least, the original code writer was aware of what he was doing.
Admin
I have seen (and then used) similar technique. Except it was multiple arrays and structure itself only contained capacity for each. Luckily it was C++ so getting pointer and bounds checking was error-free.