3 ms·
I was just going to post a comment asking why your proposed way is not the standard way. Surely free is (usually) not called that often that the overhead would
by shandor 9y ago
I was just going to post a comment asking why your proposed way is not the standard way. Surely free is (usually) not called that often that the overhead would be anything close to unbearable? This seems like a so simple method of handling most of the cases, as you point out, that I feel I'm missing some obvious downside.
- to3m 9y agoThere is no generic pointer-to-pointer in C. You get an incompatible-pointer-types warning in gcc when trying to use void * * : https://godbolt.org/g/vxWFvl https://godbolt.org/g/vxWFvl There is probably scope for use of C11 _Generic, but I don't think you could support user-defined types in that way. The usual macro problems apply when trying to do it as a macro: #define FREE(X) (free(X),(X)=NULL,(void)0) In C++ you can do something like this, which works well enough: (I don't use this specifically myself, but I have something similar for COM objects) template<class T> static void Free(T *&p) { free(p); p=nullptr; } This isn't completely useless, but it's not worth sweating blood over. Personally I think I've had more mileage from good-quality debug functionality in the memory allocator: fill newly-allocated and newly-freed regions with junk; check junk and metadata periodically; unmap regions promptly; and so on.
- ktRolster 9y agoWhen the language was made 40 years ago, the extra overhead was unbearable. Or rather, more likely, they were so focused on efficiency (because that's what you do when you have a 1Mhz computer), that the idea of making things easier for people in this way didn't cross their mind.
- pjmlp 9y agoApparently this myth never dies. Burroughs did not have this issue with ESPOL and NEWP almost 10 years earlier before C was born. Nor did the UK Navy with Algol 68RS or the multiple institutes using PL/I variants.
- ktRolster 9y agoI'm going to suggest you didn't read my whole comment.
- pjmlp 9y agoI sure did. Burroughs B5500 was created in 1961 and executed far slower and had less memory than a PDP-11, yet it was able to have a full OS written in ESPOL, an ALGOL dialect with intrisics for CPU instructions and explicit UNSAFE statements. So if a 10 year old machine by the time C was created for computers with " extra overhead was unbearable." and " what you do when you have a 1Mhz computer" was already able to execute an Algol dialect with enough performance, what does that say of C in regards to language design?