3 ms·
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. I think
by astrange 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.
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?