7 ms·
Nope it is the C committee > Once you accept that optimizing compilers do, well, optimizations Why in tarnation it is optimizing out a write to a pointer out
by raverbashing 10mo ago
Nope it is the C committee
> Once you accept that optimizing compilers do, well, optimizations
Why in tarnation it is optimizing out a write to a pointer out before a function that takes said pointer? Imagine it is any other function besides free, see how ridiculous that sounds?
- shakna 10mo agoTree shaking is pretty standard. Optimising out the write sounds fine to me - with the exception of a volatile pointer. That, there, is a mistake.
- raverbashing 10mo agoOptimizing out a write to (example) an array on the stack seems fine to me. Optimizing out a function call to a heap pointer (especially memset) seems wrong to me. You called the function, it should call the function! But it's again the C language saving time not wearing a seatbelt or checking the tire pressure for saving 10s on a 2h trip
- fluoridation 10mo agoThe whole point of the optimizer is that it can detect inefficiencies by treating every statement as some combination of simple, fundamental operations. The compiler is not seeing "call memset() on pointer to heap", it's seeing "write of variable size" just before "deallocation". For some, optimizing that will be a problem, for others, not optimizing it will leave performance on the table. There are still ways to obtain the desired behavior. Just put a call to a DLL or SO that implements what you need. The compiler cannot inspect the behavior of functions across module boundaries, so it cannot tell whether removing the call preserves semantics or not (for example, it could be that the external function sends the contents of the buffer to a file), so it will not remove it.
- uecker 10mo agoA modern compiler may also completely remove malloc / free pairs and move the computation to the stack. And I do not see what this has to do with C, it should be the same for most languages. C gives you tools to express low-level intent such as "volatile", but one has to use them.
- 112233 10mo agoStrong disagree. In C, malloc and free are functions, and I expect no magic to happen when calling a function. If malloc and free were keywords like sizeof, it would have been different.
- shakna 10mo agoIn C, malloc and free used to often be macros. As they're freely replaceable through loading, and designed for that, I would strongly suggest that are among the most magical areas of the C standard. We get a whole section for those in the standard: 7.24.3 Memory management functions Hell, malloc is allowed to return you _less than you asked for_: > The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and size less than or equal to the size requested
- 112233 10mo agoI read the text as saying the object size can be less or equal to returned memory size. Anyway, section 7 is library. As you say, replacing through loading is a common thing to do — surely compiler is not free to simply elide external library function at will? This is not C++ after all, it must be sensible
- shakna 10mo agoIf the function is equivalent to a no-op, and not explicitly marked as volatile for side-effects, it absolutely can elide it. If there is a side-effect in hardware or wider systems like the OS, then it must be marked as volatile. If the code is just code, then a function call that does effectively nothing, will probably become nothing. That was one of the first optimisations we had, back with Fortran and COBOL. Before C existed - and as B started life as a stripped down Fortran compiler, the history carried through. The K&R book describes the buddy system for malloc, and how its design makes it suitable for compiler optimisations - including ignoring a write to a pointer that does nothing, because the pointer will no longer be valid.
- uecker 10mo agoBecause it is a dead store. Removing dead stores does not sound ridiculous to me and neither is it to anybody using an optimizing compiler in the last decades.
- rectang 10mo agoIt's been many years since C compilers started making pathological-but-technically-justifiable optimizations that work against the programmer. The problem is the vast sea of "undefined behavior" — if you are not a fully qualified language lawyer versed in every nook and cranny of the C standard, prepare to be surprised. Many of us who don't like working under such conditions have just moved on to other languages.
- uecker 10mo agoI agree that compilers were too aggressive in exploiting UB, but this is not the topic of this thread which has nothing to do with UB. But also the situation with UB is in practice not too bad. While compilers broke some old code which caused frustration, when writing new code most UB can easily be dealt with in practice by following some basic ground rules (e.g. no unsafe casts, being careful with pointer arithmetic) and by activating some compiler flags. It is not anything that should cause much trouble when programming in C.