Live data from Hacker News

Memory management in C programs (2014)

nethack4.org

101–106 of 106 posts

Re: Memory management in C programs (2014)

#101
post #81

Earlier quoted context omitted.

Being able to construct without initialising is the flipside of being able to descope without deinitialising, and introduces much the same problems. It makes it much harder to use immutability (admittedly something that's hard in C++ in any case). It makes sharing objects between threads much harder to reason about, as you need to make sure init a) only happens once, but more importantly b) the effects are visible to…

Admittedly I haven't written much code with exceptions, only when using C++ frameworks that heavily rely on them, but this was always for tools where productivity was more important than performance, my actual background is game development. I have stumbled more recently over another case where the constructor/destructor paradigm in C++ is not ideal, since in some cases it 'encourages' constructing objects on the hea…

I agree that developers and code can have a lot of knowledge that lets them make allocations a lot more efficiently than the likes of glibc's malloc() (which have to handle a lot of edge cases and a lot of different workloads). As an extreme example, if you can architect around cooperating short-lived processes (Erlang-style) you can use a simple bump allocator and "dynamic" allocation is effectively (almost) free.

I wouldn't want to be doing anything custom around allocation without the support of a type system though. Having allocated-but-not-fully-initialized objects hanging around and looking exactly like fully-initialized objects is a recipe for disaster, and while it may be possible to keep track of things yourself through constant vigilance, the cognitive load just isn't worth it IMO. So if I had to do that kind of thing in C++ I'd probably look at having a custom allocator and some way to pass hints to it, rather than doing allocation and initialization separately in "normal" code. (Well, in reality I wouldn't use C++)

> Unfortunately I have stumbled over C++ frameworks which basically require to create all objects on the heap, which in my opinion is a design fault. A framework should not dictate to its users whether objects are created on the stack, on the heap, or are embedded in other objects.

Depends on the use case IMO. The flexibility of being able to control all your objects is valuable, but it doesn't come for free. One can't really generalize about what's idiomatic C++, because there's such a broad range of users.

Re: Memory management in C programs (2014)

#102

Earlier quoted context omitted.

No, C wasn't built for those things. Rather, it just so happens to be suitable for them. C was built as a higher-level alternative to assembly, for writing any kind of computer programs.

You can say that, but Dennis and Ken were writing Unix. The language got its feature set from what they needed for that application. And that application was systems programming.

Right, and Unix had things like calculator programs, spreadsheet programs, email programs.

Re: Memory management in C programs (2014)

#103
post #3

Earlier quoted context omitted.

I agree completely about C being the gateway to the computer. It really is the only way to go if you want to learn real programming. That doesn't mean I'm discounting JavaScript developers, but I like C better and it's more fun.

Slightly off topic: The way I look at it, C and C++ are the bedrock of all programming languages, because the popular compliers (llvm/clang, gcc and VisualStudio) are written in C and C++. JavaScript, for example requires a browser to compile, which requires gcc or clang to compile. Python, Rust, etc all depend on programs written in C and C++. Go is one recent exception of a popular language whose main implementatio…

> Go is one recent exception of a popular language whose main implementation is completely self hosted. Though, Go doesn't host other popular languages.

It wasn't self-hosted in the beginning, only since version 1.5 IIRC.

Re: Memory management in C programs (2014)

#104

Earlier quoted context omitted.

Slightly off topic: The way I look at it, C and C++ are the bedrock of all programming languages, because the popular compliers (llvm/clang, gcc and VisualStudio) are written in C and C++. JavaScript, for example requires a browser to compile, which requires gcc or clang to compile. Python, Rust, etc all depend on programs written in C and C++. Go is one recent exception of a popular language whose main implementatio…

> Go is one recent exception of a popular language whose main implementation is completely self hosted. Though, Go doesn't host other popular languages. It wasn't self-hosted in the beginning, only since version 1.5 IIRC.

I'm not sure something can be self hosting from the beginning in the context of computing.

Re: Memory management in C programs (2014)

#105

Earlier quoted context omitted.

You can say that, but Dennis and Ken were writing Unix. The language got its feature set from what they needed for that application. And that application was systems programming.

Right, and Unix had things like calculator programs, spreadsheet programs, email programs.

Yes, but those aren't the projects that dictated the featureset of C.

Re: Memory management in C programs (2014)

#106
post #99

Earlier quoted context omitted.

Slightly off topic: The way I look at it, C and C++ are the bedrock of all programming languages, because the popular compliers (llvm/clang, gcc and VisualStudio) are written in C and C++. JavaScript, for example requires a browser to compile, which requires gcc or clang to compile. Python, Rust, etc all depend on programs written in C and C++. Go is one recent exception of a popular language whose main implementatio…

I hate that way of looking at it because I think it's totally wrong. C (this view is usually presented as only about c) isn't fundamental or bedrock in any way. Most of our current stacks just happen to be written in it. The original Mac OS for instance was written in Pascal and C. The most popular open source compilers are written in c++. Most browsers are written in c++ not in c. And there's no reason these days yo…

> Most of our current stacks just happen to be written in it.

Sure, there's nothing special about the languages themselves, but _currently_ C and C++ are the foundation for (almost) all other languages. In theory you could rewrite llvm (or use a different complier), but I don't see that happening any time soon. That's why I call it bedrock, because it would be very hard to change.

(Though, I do wonder when llvm will stop depending on Python 2 :)

Post reply on HN