4 ms·
I don't know if I am replying to an expert programmer, who writes programs on the daily basis, but you contradict yourself: >C doesn’t have strings. It has a s
by pelmo 6y ago
I don't know if I am replying to an expert programmer, who writes programs on the daily basis, but you contradict yourself:
>C doesn’t have strings. It has a set of poorly designed functions that operate on sequences of contiguously instances of char*
Which reads as "C doesn't have strings, it has strings". You may have confused "std::string", which is a container, with the concept of strings as "array of characters". If so, then I'm clarifying, that I've meant the latter.
If you wanted to emphasize that "char" is worse than "std::string", then I do not agree with you. The reason is that, well, as I already said - "std::string" is a container, which means using it causes unnecessary inefficiency. How do you think it was implemented? C++ was written in C, by using its functions and data types.
Your phrasing of what "char" is reads like it is a some sort of a problem, however it's the way that things work on low level and by saying
>I would skip the “operations on strings” part
you would mean that you wouldn't want the person, whom I was replying to, to know about what you've wrote about "char*".
>Nowadays, I wouldn’t start with C
We don't know who you are, and what kind of programming you do, and how much you care about quality programming. The person I was replying to seemed to be interested in lower-level programming languages, so I assumed he's a some sort of a programmer who wants to understand if it's possible to write in C++ without the "Modern" stuff (which is possible, and this is what expert programmers that write quality software do).
- comex 6y ago> "std::string" is a container, which means using it causes unnecessary inefficiency. std::string is a particular data structure, consisting of a pointer to a heap allocation, a length value, and a capacity value; or alternately a "small string" that fits into the std::string structure itself. There is nothing inherently inefficient about using std::string compared to implementing a similar data structure by hand; in that sense it's a "zero-cost abstraction". You can benefit from doing it yourself if you want a different data structure that provides a different feature set, such as if you don't need dynamic resizing, or if you want to refer to existing memory directly rather than giving each string its own heap allocation. But the data structure implemented by std::string is a reasonable choice for lots of scenarios. (Alternately, you might be able to just write a more efficient implementation of std::string than the implementation in whatever standard library you're using, but most standard libraries are reasonably well optimized.) > How do you think it was implemented? C++ was written in C, by using its functions and data types. All C++ standard library implementations, and most C++ compilers, are written in C++. The standard library does of course use primitive types such as pointers, which are shared with C, but that's not the same thing as being written in C. That said, most C++ standard library implementations do use some C standard library functions, though they don't have to. In particular, std::string implementations are likely to use memcpy/memmove to copy strings, and malloc for the heap allocations. But they don't use the 'str' family of C standard library functions, the functions one would typically learn about when learning how to use strings in C. They can't, since those functions don't handle interior nul bytes, which std::string is required to support. Those functions are also unnecessarily inefficient when you know the string length, as std::string does. > write in C++ without the "Modern" stuff (which is possible, and this is what expert programmers that write quality software do). Expert C++ programmers know which parts of "modern" C++ have zero cost and can be used freely, which parts have low but acceptable cost compared to the increased memory safety and ergonomics, and which parts are best to avoid. Programmers who avoid the modern stuff entirely are mostly those who don't know C++ very well. That said, C++ is a convoluted language that is unnecessarily difficult to become an expert in, and unnecessarily difficult (but not impossible) to write efficient code in even if you are an expert.
- pelmo 6y agoI'm sorry, I don't understand why am being educated here, meanwhile I know how things work, both in C and C++. I literally got downvoted for contributing my opinion (not an advice) on what a person whom I replied wanted to know to better understand C++ without its complexity, because some people thought "that you shouldn't learn how to work with strings". Which is ridiculous. Now I regret doing so. But I've read what you wrote anyway. You contradict yourself here: >There is nothing inherently inefficient about using std::string >it's a "zero-cost abstraction" >You can benefit from doing it yourself You could've asked if I don't know something that I'm talking about, I could've clarified. > But the data structure implemented by std::string is a reasonable choice for lots of scenarios For example, these "lots of scenarios" do not include the projects I'm working on, and for a good reason. >most C++ compilers, are written in C++ I'm aware of self-hosting. The point that I was trying to convey is that, at some point, the "unsafe and poor C" was used in order to create that language, and further improve it. >Expert C++ programmers know which parts of "modern" C++ have zero cost and can be used freely And those are things that do not start with "std::" >unnecessarily difficult to become an expert in There's a difference between being an expert software engineer and expert in a particular programming language's architecture. First one involves understanding systems and know how to solve problems efficiently; second one doesn't require you writing complex software, rather learn from the docs and stay up to date with the new features without ever programming quality software.
- gumby 6y agoSide point: > I literally got downvoted for contributing my opinion I noticed that but my one upvote was not sufficient. Discussing upvoting / downvoting is discouraged on this site but I consider that unfortunate, as the votes (well, downvotes in particular) are part of the signal. Unfortunately they are also frequently used for "I disagree" rather than "this is off topic". I disagree with you but like every other user have the opportunity to express that by typing a response.
- comex 6y ago> I'm aware of self-hosting. The point that I was trying to convey is that, at some point, the "unsafe and poor C" was used in order to create that language, and further improve it. You wrote: > "std::string" is a container, which means using it causes unnecessary inefficiency. How do you think it was implemented? C++ was written in C, by using its functions and data types. Your wording strongly implies that the implementation of std::string is written in C, or at least that C++ is somehow layered over C in a way that explains the "unnecessary inefficiency" (which you could bypass by using C directly). The fact that the first C++ compilers, historically speaking, were written in C seems quite irrelevant. Perhaps you were trying to convey that the implementation of the C++ standard library is more C-like than typical C++, because it is lower-level and can't rely on the bits of the standard library that it's trying to implement? But that is also not what you said. > > Expert C++ programmers know which parts of "modern" C++ have zero cost and can be used freely > And those are things that do not start with "std::" If you are as much of an expert as you say, I assume you are engaging in rhetorical hyperbole.
- ndqc 6y ago> write in C++ without the "Modern" stuff (which is possible, and this is what expert programmers that write quality software do). Idiomatic C code is an unoptimizable mess consisting of layers of `void*` indirections, violations of strict aliasing, project-specific linked list structs and poor reimplementations of other basic data structures, `strdup()` and fixed arrays everywhere... 99% of new C projects written after 2010 and outside of the field of embedded development are made by fetishists and are neither quality software engineering nor performant, but the people who still do this stuff when they're already in a .cpp file are even worse.
- ndqc_lostpasswd 6y ago> Idiomatic C code is... Sorry, that reply of mine was rude. I get where you're coming from. C is a small language that can be quickly learned by a beginner, and familiarity with much of it is still unavoidable in C++. There's nothing wrong with enjoying the C language. I must assume that you have good reasons for avoiding modern features in your particular projects. I got triggered by that statement because of some code I've been working with recently.
- pelmo 6y agoHaha, yeah. I've read your first comment and thought to not bother with replying, as it's expected that majority of people out there are polarized by the media that tells them that "X is bad, all hail Y" (especially, modern web tech...), and I don't want to be a part of pointless discussions. You're yet another person who replied to my comments who argued against something different, i.e. you assumed that what I stated in my parent comment is that "you should program in C and enjoy doing it". In reality, I'm sharing my experience by telling that there's nothing scary about C++ once you know how to use C [so you can use the "Modern" part of C++ only when you will be in need to do that]. Many people do mistakes, but that's because they did not contribute enough time to understand the language and the principles of programming in general. Therefore, many of these people are eager to discuss about things that are not that important; imagine if they were to discuss something like how to make things better, instead of praising solutions that cause more friction (e.g. there's nothing wrong with having a programming language that gives you features like C++, but the important part is how they are implemented, because it depends on the efficiency of your program). People pretend to be experts, and miscommunication is one of the problems, which is also seen in this community, where people would argue even when all you need to do is ask a question if you didn't understand something, instead of insulting or saying that the advice is bad. >C is a small language that can be quickly learned by a beginner The C syntax can be learned, but it's not what's important. The important part is to understand how to use it, the C itself, and the concepts of programming, which is what beginners usually struggle with. >avoiding modern features in your particular projects The reason is the efficiency: how performant the program is (rendering 60 frames per second is a huge data stream, among other calculations); and the clarity (I like when my code is simple, and is not a mess).