News for hackers. Stuff that matters.
Re:C++17 Is Here: Interview with Herb Sutter by T-zex on Tuesday October 24, 2017 @09:47AM (#15541460)
I'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.
Re:C++17 Is Here: Interview with Herb Sutter by smitherfield on Tuesday October 24, 2017 @11:30AM (#15542288)
I'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.

Re:C++17 Is Here: Interview with Herb Sutter by AlexeyBrin on Tuesday October 24, 2017 @10:03AM (#15541562)
> 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 ?

Re:C++17 Is Here: Interview with Herb Sutter by pducks32 on Tuesday October 24, 2017 @10:17AM (#15541689)
I 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.
Re:C++17 Is Here: Interview with Herb Sutter by jononor on Tuesday October 24, 2017 @07:40PM (#15546160)
With 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.
Re:C++17 Is Here: Interview with Herb Sutter by AlexeyBrin on Tuesday October 24, 2017 @08:54PM (#15546497)
I 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++.
Re:C++17 Is Here: Interview with Herb Sutter by jononor on Tuesday October 24, 2017 @09:45PM (#15546730)
Sure, 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.
Re:C++17 Is Here: Interview with Herb Sutter by smhenderson on Tuesday October 24, 2017 @11:55AM (#15542529)
Sure 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.
Re:C++17 Is Here: Interview with Herb Sutter by yongjik on Tuesday October 24, 2017 @12:14PM (#15542717)
Umm, 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.

Re:C++17 Is Here: Interview with Herb Sutter by drubbo on Tuesday October 24, 2017 @09:54AM (#15541490)
Your parent poster was probably referring to std::array.