4 ms·
As someone who learned C as their first language, strings in every single language after that have felt like cheating. "What? You mean I can type an arbitrary
by frob 6y ago
As someone who learned C as their first language, strings in every single language after that have felt like cheating.
"What? You mean I can type an arbitrary string and it works? I don't need to worry about terminators or the amount of memory I've allocated? You can concatenate two strings with +?!? What is this magic?"
- macintux 6y agoYeah, every time I decide to play with C for nostalgia's sake, I immediately get hung up on just how painful everything is, especially strings. I still love C, but I'd do my best not to have to write anything serious with it again.
- unbalancedevh 6y agoIt always makes me wonder if there's some hidden overhead that I'm absorbing. When I program in C I feel like I know a lot better what the generated instructions will be. Using higher-level languages for embedded programming where resources are tight makes me uncomfortable.
- gpanders 6y agoBasically every (non-C) string implementation in existence has to do dynamic memory allocation under the hood, which is hidden from the programmer. That’s the overhead.
- 1f60c 6y ago> When I program in C I feel like I know a lot better what the generated instructions will be. If you don't know about the Compiler Explorer, definitely check it out: https://godbolt.org https://godbolt.org
- Gene_Parmesan 6y agoIn addition to the overhead from dyn alloc and the GC as someone else mentioned, there is also the size overhead that comes with every object in an OO language. The obj overhead for Java is JVM-dependent, but I believe it to be somewhere around 16 bytes. A mostly unrelated stackoverflow post I found[0] states that an empty standard string in Java occupies 40 bytes due to the normal object overhead and overhead related to the internal byte array for the char storage. Obviously what you gain in return is convenience in programming as well as runtime-enforced safety from buffer overflows. Whether this is worth it depends on what you're doing. In general, you're definitely absorbing overhead with any managed lang, although it need not be hidden. The specifics should be documented somewhere for whatever platform you're using, and most GCs are pretty tuneable nowadays. [0] https://stackoverflow.com/questions/56827569/what-is-an-overhead-for-creating-java-objects-from-lines-of-csv-file https://stackoverflow.com/questions/56827569/what-is-an-over...
- golergka 6y agoIn cases where perfomance actually matters, you usually have other tools, like StringBuilder in C#. But in my experience, 99% of code that works with dynamic strings is not perfomance-sensitive, but is often very correctness-sensitive and readibility-sensitive.
- userbinator 6y agoThere certainly is --- it's very easy to make accidentally quadratic (or worse) algorithms in languages where data structures automatically resize themselves to their contents.
- scoutt 6y agoTogether with parent comment, this is exactly how I feel every time I use something that is not C (I do mostly embedded). Like: ok, I can use a slice here, but what is a slice? what thing is it doing that I don't know? where's the catch? how does it look like in memory? is it calling a function when I access it? is it being copied or referenced?. And so on...
- astrea 6y agoComing from a hardware education and moving to an almost strictly Python career, I simply cannot enjoy the full fanciness of Python as I'm constantly worried about what weird things it's probably doing in the background. Particularly shaken after an incident of creating objects and appending them to a list, and at the end of the loop the list was the last item n times.
- kaba0 6y agoTo be honest, with today’s compilers you can’t be all that sure about the generated code. They do some insane tricks under the hood to make naively written code perform good. As per a “famous” blog post, C is a high level language.