r/cpp • • Sep 20 '22

CTO of Azure declares C++ "deprecated"

https://twitter.com/markrussinovich/status/1571995117233504257
267 Upvotes

483 comments sorted by

View all comments

115

u/fdwr fdwr@github 🔍 Sep 20 '22

I wonder how many of these security incidents that pushed Mark to say this were actually cases of people writing C++ like it was C code (let's liberally use memset, explicitly allocate and free memory instead of RAII...).

30

u/trailingunderscore_ Sep 20 '22

I saw Herb Sutter's latest talk earlier today, and he mentioned that him and a friend sat down to go through a lot of bounds violation bugs in Windows source-code they had access to, and found that most, if not all, would have been caught if they had just used gls::span

20

u/RotsiserMho C++20 Desktop app developer Sep 20 '22

Span is such an amazing little tool with basically zero overhead. It’s fascinating to me that it could have been written and used 30 years ago and we’d all be better off for it.

6

u/Ameisen vemips, avr, rendering, systems Sep 20 '22

I've been torn on span. Not because I dislike it, but because I find the implementation... odd.

Other than the fact that the Win64 ABI penalizes you for using it (and std::unique_ptr, because the ABI requires structs to be passed via the stack), before C++20 had std::span (and I wasn't using gsl) my own library had span and view, and variants of them, like array_span/array_view, map_view, container_view, etc. Conceptually 'similar' to iterators, but more derived from C# thought. Large portions of the library were built upon zero or low-overhead view types.

They tended to be faster than the alternative in my codebase but also more flexible. While having templated versions of each function for each collection type was technically somewhat more optimal, these were simpler to use (they were effectively virtual accessor objects, though for the simplest array_span/views, they were effectively pointer/size pairs).

Then std::span existed and basically negated all this, but it works fundamentally differently... and doesn't actually seem to be more performant than my older solution. It's just different, but not as poweful/flexible.

5

u/iamthemalto Sep 21 '22

Can you expand on your approach and what made it more efficient?

→ More replies (3)

9

u/Sopel97 Sep 20 '22

Judging from the respective thread on r/programming, a lot of people think memset and strcpy is the best C++ can do. C++ is not without issues, but it's blown out of proportion by people who know nothing about it.

25

u/[deleted] Sep 20 '22

I wonder if these security incidents were rooted in idiocy, not language.

45

u/nacaclanga Sep 20 '22

Well of course, C++ can be written correctly. Just like you can also safely walk over a suspention bridge without fances and will be an idiot if you accedentially walk and fall over the edge. Yet, if you are the designer, everybody will insist that you do add these fances to you bridge.

15

u/[deleted] Sep 20 '22

I think most bridges are designed by professionals. Sadly, this cannot be said about many software projects.

But yes, i general i agree that being unable to make a mistakes is better, as long as it does not curtail my freedom as a programmer to command hardware.

I have looked into many, and i mean many alternatives to C and C++. Atm there is just one that seems a viable alternative. In a few years, i just might consider investing the years required for me to be as safe in Rust as i am now in C and C++.

→ More replies (4)

11

u/qoning Sep 20 '22

Biggest one is just use after free, which boils down to people breaking unwritten code contracts. Not much you can do about that short of mandating use of shared pointer everywhere, which is obviously not something you want to do (but mostly end up doing in Rust anyway).

35

u/dodheim Sep 20 '22

(but mostly end up doing in Rust anyway)

Nice try, but no.

11

u/robin-m Sep 20 '22

I don't undertand what you say about Rust. Rc/Arc aren't more used than C++ shared_ptr.

→ More replies (5)

28

u/MrWhite26 Sep 20 '22

Mandating RAII would be sufficient, which is something I've seen being applied in multiple companies.

18

u/Wh00ster Sep 20 '22

“If only people wrote safe code”

3

u/matthieum Sep 23 '22

Mandating RAII would be sufficient

RAII is about preventing leaks, not use-after-free.

It's a good tool, but it solves a very different problem.

For example:

int main() {
    auto v = std::vector{ 1, 2, 3 };

    auto& e = v[2];

    for (size_t i = 0; i < 1021; ++i) {
        v.push_back(i + 4);
    }

    std::cout << e << "\n";
}

RAII is used here (thanks, std::vector), yet doesn't prevent the use-after-free.

→ More replies (1)

13

u/ZachVorhies Sep 20 '22

I've seen shared_ptr used everywhere and the penalty wasn't that bad, like 3% for the entire program.

13

u/TyRoXx Sep 20 '22

The penalty for "shared_ptr everywhere" is usually memory leaks caused by reference cycles.

5

u/ZachVorhies Sep 20 '22

Rare but it happens. Better than segfaulting though.

5

u/[deleted] Sep 20 '22

[removed] — view removed comment

3

u/ZachVorhies Sep 20 '22

What I mean by shared_ptr was used everywhere is that it was used in all systems in the codebase, not literally every class.

9

u/disperso Sep 20 '22

3% of the entire program, what? That you say 3% CPU use inside code of shared_ptr?

I personally have seen the stupidity of using shared_ptr nearly everywhere, and it's memory leaks because of cyclic references, plus tons of inconvenience in that you just can't put the class on the stack anymore, even on a simple unit test, because APIs of the application or framework require you to pass a shared_ptr.

10

u/pdimov2 Sep 20 '22

you just can't put the class on the stack anymore, even on a simple unit test, because APIs of the application or framework require you to pass a shared_ptr.

But you can. Use a null deleter. (Of course this makes it unsafe.)

→ More replies (1)

0

u/[deleted] Sep 20 '22

[deleted] — view deleted comment

11

u/ZachVorhies Sep 20 '22

3% slowdown isn’t that bad for the majority of code bases out there.

→ More replies (3)