- 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
Edit Admin
Wow, the memory management is actually the first red flag I noticed.
In C/C++ you either have a factory or you have a buffer. You don't mix them for various architectural reasons but also because you can end up with two different memory management implementations for example in the main app and libraries which would result in super weird memory issues that are super annoying to detect - often worse than multi threading issues.
Admin
Actually, it isn't accessing "out of bounds" because the declaration says there are 1000 elements, and since there is an initializer, the remaining elements do get initialized to 0.
Edit Admin
Wow! This is the first time ever I see a piece of code and the first adjective that comes to my mind is "cursed". Just in case, don't read it out loud because it gives the impression it might summon something nasty. :D
Edit Admin
Um.
No, it's not out-of-bounds. The extra 10 entries are all full of zeroes, specifically
{ '\0', '\0', '\0' }
in each one, seeing as how the array is declared at 1000 entries.Admin
Should be
Edit Admin
I think I figured out the naming.
dl
isdata_len
, but it's used as the index intoan
, so I think it's the length of something that's being converted to a string, probably for display to a user. Thenlen_len
is part of a structure calledcfg
, so it's probably some kind of configuration setting that means "the display length of the length property of the data".Or something.
Admin
"And frankly, why we couldn't just use itoa or a similar library function which already does this and is probably more optimized than anything I'm going to write."
I have long since concluded that a simple sense of humility can be a programmer's greatest strength.
Edit Admin
That break on the 14th line won't do anything, unless this code is inside a loop that we don't see. Assuming it's not, then execution will proceed merrily along.
Edit Admin
ha ha ha ha nice try, Remy, but I've been through enough that you couldn't pay me to examine that code. Those days are GONE.
I like a good CodeSOD as much as the next reader, but I have my limits.
Admin
The icing on the cake is putting the string terminator in the wrong place.
Edit Admin
Even if it's in a loop it won't do anything. It's for breaking out of the
switch
.Why we have a switch statement with only one
case
is the new question, unless Remy chose not to show the rest of it.Edit Admin
It seems that all of you assume the memory gets zero-filled. I don't think this is guaranteed.
Edit Admin
It's C, which guarantees that initialised-but-not-fully aggregates (arrays, structs) are "zero"-filled in the parts that aren't initialised.
"zero" in quotes because it's not necessarily just a memset-to-zero, if the in-memory representation of e.g. floats, doubles or NULL pointers is not the all-zeroes bit pattern. (The main culprit these days would be IBM's iSystem machines, formerly AS/400, where the bitpattern for NULL is not all-zeroes.)
Admin
I had a hunch, and yes indeed: the range 990-999 is unassigned in the north American area code plan and reserved for future expansion.
Admin
I love how the variable is named 'll', which looks a lot like 11, which makes the condition chain really weird at first glance. If 11 < 2, else if 11 < 3, etc...
Variable naming is an art, and this one's about on the level of a kindergartner's finger-painting.
Admin
C89 says, in relevant part: If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0.
What really hurts my eyes in that code is the inconsistent spacing around "operators." Working in a company that automates uniform code formatting has spoiled me.
Edit Admin
We have the right to know the name of this company.
Edit Admin
itoa was one of the very first bits of code I ever got paid to write. I was a lot better-looking then.
Edit Admin
@RIchard Brantley ref
Actually, IMO the greatest strength is knowing what's already built into your language / dev environment / framework / libraries, etc.
Far more wheels are reinvented from ignorance than from hubris.