4 ms·
> Most obviously in C, we note that a malloc() implementa- tion is usually written in C—or rather, in a subset of C that lacks malloc() since malloc() is mandat
by RcouF1uZ4gsC 6y ago
> Most obviously in C, we note that a malloc() implementa- tion is usually written in C—or rather, in a subset of C that lacks malloc() since malloc() is mandated by the C standard.
Not always. Google’s tcmalloc is actually written in C++.
I actually would have agreed with a lot of this in 2017. However, in the past several years, I think Rust has been a game changer. It can interface with the C ABI. It can access the same low-level abstract machine that C can. It doesn’t have garbage collection or virtual machines. And it provides memory safety out of the box. In addition, features such as strong types and pattern matching help less logical bugs as well (like the compiler checking that you did not forget one arm of an Enum).
This is born out now that a lot of security facing software is starting to do at least part of their internals in Rust, where they had been in C before.
I think Rust is and will be even more in the future a game changer in how we write the foundation programs and libraries that the computing world is built on.
- astrange 6y agoWhat they're saying is that malloc() can't be implemented in C, because C defines malloc() as having behavior that's not possible in C's memory model. I think it is more possible in C++ due to placement new, though tcmalloc is probably still relying on language extensions.
- ttt0 6y agoWhat? Linux is written in C and it there is no malloc in the kernel. Just don't include standard library.
- astrange 6y agoLinux isn't written in C either, it's written in C with a lot of language extensions. The ability to implement malloc() is just another language extension where the compiler promises to ignore the type aliasing issues. Implementing memory barriers (pthread_mutex_lock()) used to be strictly speaking impossible in C too, but it's possible now that it has a defined memory model.
- mhh__ 6y agoIgnoring that the kernel has lots of raw machine code in it, "not possible" and undefined or implementation defined behaviour are different things either practically or in terms of programming language semantics.
- kevin_thibedeau 6y agoI don't know why you think magic is needed to implement malloc(). It can be readily implemented in C. It controls a range of memory and doles out chunks upon request. Doing that in a system with an MMU requires hardware interaction to handle virtual memory but that isn't outside of the wheelhouse for C.
- pjmlp 6y agoIt is outside of what ISO C defines and requires either language extensions or external Assembly.
- astrange 6y agomalloc() in C returns a new memory object each time you call it (I forget the exact wording here). There is no operation in the C standard that creates new memory objects; the only way to do it is by calling malloc(). And obviously, this doesn't let you implement it.
- kevin_thibedeau 6y agoYou don't have to "create a new memory object". C doesn't say how malloc() is implemented. It is perfectly valid to have a statically defined array and have malloc() pull from that. The majority of the computers in the world do this. You're conflating limitations imposed by layers of OS infrastructure with limitations of C.
- astrange 6y agoI'm not talking about any OS concept, but about the concept of how memory works in the virtual machine defined by the C standard. That implementation isn't compliant just because it works; if an undefined behavior checker could see into the malloc implementation, it would see that it's returning pointers inside the same object, which conflicts with the (impossible to implement) rule that malloc returns pointers to different objects. The practical consequence of this is malloc is kept in a different library and not inlined into callers, and asan doesn't work with custom allocators unless you tell it they behave like malloc.
- aidenn0 6y ago> What they're saying is that malloc() can't be implemented in C, because C defines malloc() as having behavior that's not possible in C's memory model. Is that due to aliasing rules?