• (nodebb)

    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.

  • Anonymous (unregistered)

    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.

  • (nodebb)

    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

  • (nodebb)

    And, given that the result is an out-of-bounds array access

    Um.

    char an[1000][3] =

    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.

  • Scragar (unregistered)

    we start dividing my 1000

    Should be

    we start dividing by 1000

  • (nodebb)

    I think I figured out the naming. dl is data_len, but it's used as the index into an, so I think it's the length of something that's being converted to a string, probably for display to a user. Then len_len is part of a structure called cfg, so it's probably some kind of configuration setting that means "the display length of the length property of the data".

    Or something.

  • Richard Brantley (unregistered)

    "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.

  • (nodebb)

    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.

  • (nodebb)

    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.

  • Tinkle (unregistered)

    The icing on the cake is putting the string terminator in the wrong place.

  • (nodebb) in reply to RogerC

    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.

  • (nodebb)

    It seems that all of you assume the memory gets zero-filled. I don't think this is guaranteed.

  • (nodebb) in reply to davethepirate

    It seems that all of you assume the memory gets zero-filled. I don't think this is guaranteed.

    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.)

  • Duke of New York (unregistered)

    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.

  • Brian (unregistered)

    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.

  • Duke of New York (unregistered) in reply to davethepirate

    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.

  • (nodebb)

    We have the right to know the name of this company.

  • (author)

    itoa was one of the very first bits of code I ever got paid to write. I was a lot better-looking then.

  • (nodebb)

    @RIchard Brantley ref

    I have long since concluded that a simple sense of humility can be a programmer's greatest strength.

    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.

  • Guessed (unregistered)

    If you think itoa() is a C library function, you may be a Windows programmer.

  • (nodebb)

    At least it's not dates. Those are hard.

  • (nodebb)

    I knew more than a few C compilers back in the day which "standard" library did not have an itoa. Mostly embedded development platforms on very constrained processors. The exact kind where that lookup table would be the last thing one would use because memory was so precious.

  • (nodebb) in reply to Barry Margolin

    It's for breaking out of the switch.

    The one on line 14 isn't. There are two break statements in this code.

  • (nodebb)

    In my C reference (K+R second edition) the library has "atoi", but "itoa" is given as a sample code. It's a bit more capable version of the loop version in this article's code.

  • (nodebb)

    Who knew integer division by 10 (twice, unless the compiler can optimize it) was so expensive as to warrant a huge lookup table?

  • (nodebb) in reply to Guessed

    Nope, pretty much all compilers came mit C/C++ libs including iota - from the Amiga C reference compiler, to the IBM AIX compiler to the Watcom-C compilers. The only notable exception beside a few small ones and microcontroller compilers is gcc, so I guess that's where you are coming from.

    And why iota is not part of the "standard" was a discussion 20 decades in its making due to the K&R reference implementations being a mess and every compiler vendor implementing their own version. So basically not including it in the first "C standard" (+ drafts) was considered the compromise and when the actual proper standard came around with C99, everyone was pretty much happy with the status quo. Well, technically nobody wanted the endless discussions all over again. No idea how the story went afterwards, because I had by that point professional transitioned, but I would be actually surprised it anyone would have opened that Pandoras box again.

  • (nodebb) in reply to Guessed

    Nope, pretty much all compilers came mit C/C++ libs including iota - from the Amiga C reference compiler, to the IBM AIX compiler to the Watcom-C compilers. The only notable exception beside a few small ones and microcontroller compilers is gcc, so I guess that's where you are coming from.

    And why iota is not part of the "standard" was a discussion 20 decades in its making due to the K&R reference implementations being a mess and every compiler vendor implementing their own version. So basically not including it in the first "C standard" (+ drafts) was considered the compromise and when the actual proper standard came around with C99, everyone was pretty much happy with the status quo. Well, technically nobody wanted the endless discussions all over again. No idea how the story went afterwards, because I had by that point professional transitioned, but I would be actually surprised it anyone would have opened that Pandoras box again.

  • CmdrShepard (unregistered)

    No, I can confirm that embedded libraries especially in bankong industry were missing a lot of what you consider "standard" C functions. Anything not present was implemented by junior devs horribly and when hardware and SDKs were upgraded it was all ported and it is still a ticking time bomb in some PoS and ATM systems in some 3rd world shithole.

  • Officer Johnny Holzkopf (unregistered)

    Regarding the len_len: For a moment I thought it's something like a magnitude, e. g., input is "12345", that is 5 digits, length of length 5 is 1 (1 decimal position); "123456789012345" is 15 digits, length of length 15 is 2 (2 decimal positions)... but then I noticed that people writing C are sometimes considered morons because of the "experts" who write C even though they cannot C...

  • (nodebb) in reply to MaxiTB

    And why iota is not part of the "standard" was a discussion 20 decades in its making

    I knew C had been around for a long time, though I hadn't realized that talks about it began just 13 years after the War of 1812.

  • (nodebb) in reply to gordonfish

    Obvious typo is obvious.

    Addendum 2025-05-16 16:41: Also: Rain was actually cold. Even though I claimed today it's sunny and warm. Just want to make sure that nobody got confused by that one too :-)

  • Tim (unregistered) in reply to gordonfish

    Actually Babbage created C to program the Difference Engine. Now you know why it was never completed. ;-)

Leave a comment on “itouhhh…”

Log In or post as a guest

Replying to comment #680171:

« Return to Article