3 ms·
> Functions like memcpy or malloc cannot legally be written in C without breaking this behavior > Also not true. You cannot implement malloc in C because mall
by astrange 11y ago
> Functions like memcpy or malloc cannot legally be written in C without breaking this behavior
> Also not true.
You cannot implement malloc in C because malloc always returns a pointer that's not a part of an existing allocation. The malloc in libc has special dispensation from the compiler to do this (GCC "malloc" attribute).
Similarly you can't implement pthread mutexes in C because they imply optimization barriers (all global memory might change) that a C function with a visible implementation wouldn't have.
- dsfuoi 11y agoYou can easily implement malloc. I don't understand what relevance with this your paragraph has. C11 has atomics and all the necessary primitives, fences, locking, etc..., to implement pthread_mutex, including its own mutex.