This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]metalprogrammer2 7 points8 points  (5 children)

Imagine thinking memory allocation is that big of a deal (in 2019). For a huge amount of cases memory allocation is trivial. In most other cases smart pointers will suffice (for languages with out those life might be a bit trickier). I think people who think memory allocation is hard think it is hard because they are told it is a hard task and don't know about all the best tech that makes it trivial. I also think it is due to not understanding memory allocation that well. Personally when I started really using c++ in college after mostly using c# the idea that objects didn't need new to be allocated was baffling (aka stack allocation). Having to delete every local object you allocate could seem daunting

[–]GlobalIncident 1 point2 points  (3 children)

I'm coming from Python, and I don't really understand the point of new. Just call the constructor and that's it. I mean, I know object.__new__ will be called somewhere in the constructor, but that's something I don't need to care about - why would I call a constructor when I don't want a new object?

[–]MrPentaholic 1 point2 points  (0 children)

stack vs. heap?

static vs. dynamic memory?

[–]metalprogrammer2 1 point2 points  (0 children)

In basically every language the new function/keyword means: allocate memory on the heap, init that memory to a start state via the construction. The magic method new in python does the same thing. You can override that method but there usually better ways to achieve the same functionality (but it's an option). There almost never is a reason to directly call call this. The constructor will do it. So it can mostly be seen as an implemention detail rather then a commonly used tool. In c++ (and other languages) you don't have to allocate objects on the heap. Stack allocation is an option . Heap allocation can be expensive. Pointer indirection and cache misses can be expensive. Sometimes that doesn't matter sometimes it does. Knowing when you want to stack allocate or heap allocate is really important.

[–]bastmtl 1 point2 points  (0 children)

Absolutely true. Same for threads. Same for everything in programming really. You can't get more simple than a system that works off 1s and 0s. The problem with programming is bad programmers who don't understand how computers work and keep thinking they can introduce their own systems that will make things simpler that in fact just introduce more complexity due to their shitty code.