this post was submitted on 19 Mar 2024
145 points (92.9% liked)

Programming

17025 readers
88 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
145
submitted 6 months ago* (last edited 6 months ago) by [email protected] to c/[email protected]
 

Python is memory safe? Can't you access/address memory with C bindings?

top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 6 months ago

Oh boy, the Biden admistration is in their nerdy programming language phase. I'm scared for when the administration gets into WM vs DE and warns about the bloat of not using suckless software.

[–] [email protected] 4 points 6 months ago

Look, there's a thing called safety-catch and that's why my son can play with semi-auto rifles.

[–] [email protected] 14 points 6 months ago

I'd love to see Bjarne Stroustrup being grilled by Congress about this, and have to explain shared pointers and reference counting to the same people who didn't understand how Facebook made money.

[–] [email protected] 7 points 6 months ago (6 children)

So how fucked am I for starting to learn cpp as my first language, or is this a later down the road thing to worry about?

[–] [email protected] 2 points 6 months ago* (last edited 6 months ago)

It's a great language to learn. The memory safety specifically. If you only use memory safe languages, you won't know anything about how the memory is handled in the background. Start breaking and abusing memory, it's the best way to learn why memory safety is important, what it's doing different, and how it's prevented.

Getting deep in the trenches with memory allocation is the best way to learn, since all memory safe languages do this as well, they just hide it. It's extremely useful to know what's going on behind the scenes and is fundamental knowledge that applies to all programming languages, past and present, no matter how much they hide it.

[–] [email protected] 9 points 6 months ago

If you can write correct C++ you'll be able to write Rust code that compiles first time. Don't stress, you're learning the good stuff.

[–] [email protected] 3 points 6 months ago

There will be plenty of jobs in c++ in the foreseeable future, so it's not a bad language to know from that perspective. I don't know if it's the most pedagogical language to learn otoh, python is a better language for getting comfortable with the basics, c is better when it comes to learning a (slightly wrong but close enough) mental model of how a computer works under the hood, and there are many better languages to learn if you want to learn good approaches to thinking about problems.

Maybe you are leaning c++ because you want to work on something specific that c++ is primarily used in, and in that case go ahead with that project. I think having something tangible that you want to work on is great when it comes to learning programing and that's worth more than picking the "best" language. Besides, you can always learn different languages later in your career if you want/have to.

[–] [email protected] 8 points 6 months ago

So how fucked am I for starting to learn cpp as my first language, or is this a later down the road thing to worry about?

I don't see why you should be concerned, except that no professional software developer is limited to use one specific programming language.

Even if you pay attention to the disaster prophets in the crowd, which are mainly comprised of fanboys implicitly and explicitly promoting their pet language/frameworks, C++ dominates all aspects of the computing ecosystem, which means that in the very least the whole world needs to maintain existing C++ projects to continue to work. See COBOL for reference.

[–] [email protected] 5 points 6 months ago

Ah that's an easy one - what would you like to do?

My first uh... "language" was bourne shell. Not because I thought it was a cool language, but because that's what let me do things I wanted to do at the time: automate heaps of Linux, BSD stuff.

There are heaps of libraries and applications where C++ is the choice e.g. video games. My friend is great at Javascript because he loves web browser tech.

Don't stress - have fun! :)

[–] [email protected] 6 points 6 months ago* (last edited 6 months ago)

I think it's a good first language to learn. Because it's a lower level language than most of the popular ones these days, you will learn a lot of interesting concepts and problems you wouldn't otherwise get or even need for higher level stuff - like memory management, allocation, garbage collection and cleanup, and pointer references - but which give you a better understanding and appreciation for what's in the black box.

But you also learn a lot of transferable concepts like typing, mutability, objects and object oriented design, polymorphism, etc. And there are still a ton of jobs where C++ knowledge is required or at least useful.

I have barely touched it over the years since first using it, but patterns and approaches I learned in C++ have come in handy for me a lot when troubleshooting and when reading other people's code, and being able to recognize who is going to be a helpful person to talk to when I run into problems I can't quickly figure out.

[–] [email protected] 28 points 6 months ago (2 children)

Biden administration seems oblivious of the strengths of contemporary C++

Well ok, but the concern is about the weaknesses, Mr. Stroustrup.

[–] [email protected] 5 points 6 months ago* (last edited 6 months ago)

Stroustrup to congress: "You expect me to talk?"
Congress: "No, Mr Stroustup, we expect your language to DIE!"

[–] [email protected] 1 points 6 months ago (3 children)

Well ok, but the concern is about the weaknesses, Mr. Stroustrup.

I don't think these discussions on "weaknesses" come from a place of intelectual honesty. None of these arguments even touches the fact that there are already a myriad of freely available static code analysis tools and memory profilers that do a very good job catching memory safety issues.

For some unexplainable reason, these criticisms of C++ always focus on a single strawman: these tools do not exist and no developer in the world cares about the topic.

[–] [email protected] 2 points 6 months ago

a single strawman: these tools do not exist and no developer in the world cares about the topic

I haven't seen anyone make the argument that denies these things exist - it's that the existence of these tools are even necessary to safeguard the language in the first place is the argument. And then on top of that, you'll additionally need a shop that is even allowed the time to properly utilize these tools and implement their usage as standard practice within the company culture.

That there are alternatives which remove (significantly more) footguns is the overall point. Work in one of these other languages so e.g. dumb-ass PMs don't even have the option of pilfering the time it takes to code safely, as it would already be baked in.

[–] [email protected] 5 points 6 months ago

There is a lot of fanboying in discussions like these, so I understand if you're weary of that. That said I don't think static analysis tools are a very good point of comparison for (what I'm assuming that you're referring to) Rusts ownership system.

While static analysis tools certainly can be useful for some classes of errors, there are types of errors that they can't catch that the borrowchecker can. This is because the language are built around them in Rust. Rusts lifetime analysis is dependent on the user adding lifetime annotations in certain situations, so since c++ doesn't have these annotations static analysis tools for c++ can't benefit from the information these annotations provide.

Furthermore, c++ suffers from being an old language with a lot of features. Legacy features can allow for various loopholes that are hard for a static analysis tool to reason about.

C++ static analysis tools can find errors, but Rusts borrowchecker can prove the absence of errors modulo unsafe code.

That said, I don't have any good data on how much of a problem this is in practice. Modern c++ with a CI-pipeline doing static analysis and forbidding certain footguns is safe enough for most contexts. Personally, I'm exited about Rust more because I think that it's a nicely designed language than because of its safety guarantees, but it doesn't really have the ecosystem support for a lot of things, like gamedev or ui at the moment.

[–] [email protected] 3 points 6 months ago* (last edited 6 months ago)

A huge number of exploits rely on unsafe memory bugs.

The fact you have to rely on external tools to manage these issues perfectly illustrates that the language is not memory safe without extra work and it's easy to miss things.

It seems much more sensible to just use a language that is.

[–] [email protected] 10 points 6 months ago (1 children)

Sounds like he's just mad something he made is being replaced with something that is better in every conceivable way

Sorry bud but most people are focusing on rust, not python, and you've been lapped by them several times over.

[–] [email protected] 2 points 6 months ago (4 children)

I'm just a modder and primarily use C, but started with BASIC and then C++; I am curious, without knowing anything other than the name and it's apparent growing popularity: What makes Rust so appealing? And if I was interested in trying to learn Python again, would it be better to just learn Rust instead?

[–] [email protected] 1 points 6 months ago* (last edited 6 months ago)

Rust is the only language with the same low-level memory model of C/C++ (no garbage collector, focus on zero cost abstractions, etc) while also being memory-safe (like nearly all popular modern languages besides C/C++). Before Rust, you often had to choose between memory safety and performance.

[–] [email protected] 3 points 6 months ago

Rust has a lot going for it beyond just the safety thing: excellent package manager, powerful trait system and generics, helpful compiler errors.

The whole language is designed to help you avoid making the programming mistakes people tend to make, not just the borrow checker and memory safety.

[–] [email protected] 2 points 6 months ago

It feels like the last language one will need to learn.

It has an improved C-style syntax (if statement is similar to if expression), it has algebraic type system (enums can contain nested data) and 99.9% of the time you can write in safe mode where you are guaranteed not to segfault.

[–] [email protected] 3 points 6 months ago

I guess I kind of see it like this: I wouldn't touch C or C++ without a 10-ft pole. Rust is my 10-ft pole.

That being said, I think python occupies a very different space from rust and allows for super rapid prototyping so I wouldn't conflate the two

[–] [email protected] 32 points 6 months ago

He can get mad all he likes but there's a reason the Linux kernel's experiment with rust is going much smoother than C++ (which only lasted a literal week btw)

load more comments
view more: next ›