6 ms·
My opinion is that there is no "right way" of doing C++, unlike, say, python. You can write a kernel in C++, or you can do high level UI, or a game engine, and
by GuB-42 9y ago
My opinion is that there is no "right way" of doing C++, unlike, say, python.
You can write a kernel in C++, or you can do high level UI, or a game engine, and these will be very different, you can almost say you are using a different language.
For example, to allocate an array, you can use a vector, array, new, or malloc. Most people will tell you to use a vector, but a vector uses a bit more memory than an array, so you might want to use this instead, but you may not want to link with the STL at all, so you go with new, but if you don't want to deal with exceptions or just want a block of raw memory, you may want to go with malloc instead.
Just use the new features if it makes life easier for you, that's the idea. As for linters, I know that cppcheck can warn you of C-isms (ex : C-style casts) but I don't know any of them that target modern C++ variants.
- T-zex 9y agoI'm just a hobbyist, but "new" and especially "malloc" are not "right way of doing C++". Use of array is non idiomatic either. Instead you should always aim for vector and smart pointer "factory" methods make_shared or make_unique.
- drubbo 9y agoYour parent poster was probably referring to std::array.
- AlexeyBrin 9y ago> I'm just a hobbyist, but "new" and especially "malloc" are not "right way of doing C++" How do you think the std::vector is implemented under the hood ?
- pducks32 9y agoI think what he's saying is that using `std::vector` is better than using pointers and `malloc` from the point of view of doing simple things. (Simple is not the right term but can't think of a better word). I mean 80% of times.
- smhenderson 9y agoSure but they are implemented by C++ specialists that work on this code all the time. I'd trust them to use malloc more correctly and bug free than I'd ever trust myself to do. As long as we're not talking embedded here I'd rather rely on work already done by experts than reinvent the wheel, perhaps poorly.
- jononor 9y agoWith that line of argumentation, why not use sbrk() or poke the syscall directly? :) I'd say prefer the safest, highest-level of abstraction that you can get away with. Which would generally mean std::vector over new/malloc.
- AlexeyBrin 9y agoI have no problem with the use of std::vector (in 90% of cases it is all you need), my problem is with the new trend of shunning any use of new/delete or naked pointers in modern C++.
- jononor 9y agoSure, dogma is not very useful. But when people ask "what should I use" saying "well, there are valid cases for everything" (like someone else in the thread basically did) does not help much either.
- smitherfield 9y agoI'd pretty strongly disagree with the notion that stack or static-allocated arrays (especially `std::array`) aren't idiomatic C++; rather it's (unnecessarily) allocating everything on the heap that's the anti-pattern, since it defeats the whole purpose (fast, fine-grained, deterministic memory model) of using C++ in the first place. (And implies lots of memory leaks and global state). Also, `new`/`delete` aren't unidiomatic when strictly adhering to RAII and - Writing exception-safe code (with the `new (std::nothrow) T` form). - Writing custom smart pointers or data structures, or high-performance containers (especially with C++17 aligned `new`/`delete` and C++14 sized `delete`). - Using memory pools (with placement `new`/`delete`). `[std::]malloc`/`[std::]free` aren't necessarily unidiomatic either, such as - When using a C API (or one in pre-standard C++ or a third language) which is incompatible with `new`/`delete`. - When you need `[std::]realloc`, and for whatever reason a `std::vector` isn't an acceptable substitute. - In embedded or other environments that don't provide a STL.
- yongjik 9y agoUmm, no. A vector allows you to dynamically change the length of an array. If you need it, then you pretty much need a vector (or some other kind of smart container). Otherwise, there's no reason to prefer a vector to C-style array. Also shared_ptr's make the ownership relation harder to understand. (Also there's additional synchronization cost for copying pointers.) Of course there are situations where you need them, but in general, I think it's better to avoid them.
- nextos 9y agoI was quite invested in C++ 1998 back in the day, but didn't keep in touch with the language much after 2003. I am now back into high-performance scientific computing, and I am glad to see C++ has evolved much, in pretty good ways. However, is it worth getting back into C++ when things like Julia and Rust are around? Or are these still too immature?
- pjmlp 9y agoDepends on the use case. For me, as native companion to Java and .NET stacks, Rust still cannot take C++'s place regarding mixed debugging experience, IDE integration, GUI designers support, distribution of binary binaries and most important making team mates or customers (specially their IT) accept yet another tool into the projects. For other people Rust is already mature enough and they are delivering production code with it. In any case, until Julia and Rust stop using LLVM as their backend, being confortable with C++ is a good idea.
- nextos 9y agoMy use case is rapid development of new statistical methods. Julia seems ideal in some ways, as performance can be quite close to C / C++ / Fortran, while the code is more high-level. I don't care about GUIs, distribution, teamwork or maintenance. Julia seems promising, and there are some nice usage stories already: https://www.intel.com/content/dam/www/public/us/en/documents/presentation/julia-in-parallel-and-high-performance-computing.pdf https://www.intel.com/content/dam/www/public/us/en/documents... I am quite proficient with JVM-based stacks (Java, Scala & Clojure). But the level of performance, especially in terms of memory usage, cannot come anywhere close to C++.
- kllrnohj 9y ago> My opinion is that there is no "right way" of doing C++, unlike, say, python. There are, however, definitely wrong ways of doing things in C++. There's quite a few things in the STL that should have long since been deprecated if not outright removed. Simple example is std::map. Absolutely horrible data structure. std::unordered_map is what you actually want, but to a beginner nothing really tells you that. You just think "I need a map, oh hey there's a std::map, perfect!" and go about your day completely unaware of how awful a data structure you just chose. Another example that's a mistake made in C++ hello worlds of all things is std::endl. std::endl does not mean "end line", it means "\n + flush" (specifically '\n' - it does not do '\n\r' or '\r\n' conversions at all, the underlying file system does that). And flushing on every new line only makes sense for line-buffered things like std::out, but std::out already flushes on '\n' character. So std::endl in practice just leads to a ton of unnecessary & unexpected flushes. > so you go with new, but if you don't want to deal with exceptions or just want a block of raw memory, you may want to go with malloc instead. There's actually very little reason to ever use malloc. If you globally don't want exceptions just use -fno-exceptions. If you don't want exceptions in the small scope you're working in then use std::nothrow, eg: "new (std::nothrow) int[1024 * 1024]". As for why you do this over malloc the reason is simple - it avoids overflow bugs. new int[size_t] never overflows. malloc(size_t * sizeof(int)) - well that overflows trivially, and that overflow results in security bugs. Raw malloc would arguably be another case of "this is just wrong C++" rather than "this is a perfectly valid alternative". Raw free would belong in this case except for calloc, which is still very useful (and avoids the common overflow bug that malloc has). malloc's only saving grace is realloc, but now you're pretty deep into a specific edge case.
- CoolGuySteve 9y agostd::map will often have better cache coherence, especially if you're using a custom allocator or have a small data set. Also not sure what you mean when you say operator new never overflows. Can you elaborate? It would be nice if C++ gave an operator new/allocator that put the object on the end of a page boundary so that the hardware could catch any overflows. Sort of like how the atomic operators are cross platform but hardware specific.
- Analemma_ 9y ago> My opinion is that there is no "right way" of doing C++, unlike, say, python. As a longtime hater of C++, I do want to point out that this is one of my big issues with it. Every time I mention my various gripes with C++, I get the same response from the apologists: "Well, if you just do C++ the right way, that's not an issue" Fine! Great! Then just tell me what the right way is! But no one can (or if they do, then someone else comes along and immediately tells them they're wrong). If there were some agreed-on idiomatic subset of C++ that avoided all the pitfalls, or even just a book that everyone agreed was "good", that would go a long way toward warming me on the language. But so far as I can tell, no such thing exists.
- santaclaus 9y ago> But so far as I can tell, no such thing exists. The core guidelines are working to be precisely that.
- steveklabnik 9y agoNot precisely that. The Core Guidelines are great, but they're attempting to solve a subset of the issues, not all of the issues.
- gmfawcett 9y agoHaving worked with Python for 16+ years, I disagree that there's a "right way" of using Python, in any meaningful sense.
- jononor 9y agoIn practice people do tend to disagree less about what is good code and not in Python versus C++?
- gmfawcett 9y agoI don't think anyone could answer that question, except with anecdotes. Personally, I think there is plenty of room for disagreement about code "goodness" in Python, C++, or any other general purpose language.