Really, don't do this, it's a portability and safety nightmare (aside from C not being memory safe already). C programmers are better off with either of these two techniques: * Use __attribute__((cleanup)). It's available in GCC and Clang, and we hope will be added to the C spec one day. This is widely used by open source software, eg. in systemd. * Use a pool allocator like Samba's talloc ( https://talloc.samba.org/…
> It's available in GCC and Clang, and we hope will be added to the C spec one day. This is widely used by open source software, eg. in systemd. It’s odd that the suggestion for a feature lacking in C is to use a non standard but well used supported path. c’s main selling point (IMO) is that it _is_ a standard, and relying on compiler vendor extensions kind of defeats the purpose of that.
Stupid Smart Pointers in C
111–120 of 174 posts
Re: Stupid Smart Pointers in C
#112IMHO trying to emulate smart pointers in C is fixing a problem that shouldn't exist in the first place, and is also a problem in C++ code that uses smart pointers for memory management of individual objects. Objects often come in batches of the same type and similar maximum lifetime, so let's make use of that. Instead of tracking the individual lifetimes of thousands of objects it is often possible to group thousands…
Re: Stupid Smart Pointers in C
#113Earlier quoted context omitted.
My C programs never consumed gigs of memory. So I (like many others I assume) made a memory manager and never freed anything. You'd ask it for memory and it kept a list of various sizes it allocated and returned what you needed to be re-used. Freeing and allocating is slow, and error prone, so just avoid it!
Well, it's basically an implementation of a memory allocator. But how did you determine what you could re-use? That's the hard problem, one that's equivalent to calling free() at the right time.
Re: Stupid Smart Pointers in C
#114Re: Stupid Smart Pointers in C
#115Re: Stupid Smart Pointers in C
#116Earlier quoted context omitted.
Well, it's basically an implementation of a memory allocator. But how did you determine what you could re-use? That's the hard problem, one that's equivalent to calling free() at the right time.
When something would normally be freed, it calls the memory manager's version of free(), which zeroes the memory and adds it back into the appropriate available list.
Re: Stupid Smart Pointers in C
#117Earlier quoted context omitted.
That sounds like the kind of code that you want to be done with and never touch again. You can only dream of it not being buggy or catching up with the newest standard.
No it is more like the 10000 lines of code running in your washing machine, you will probably be updating it in the next year revision of the product. It is quite common for this code to have all variables be global and just not have any heap allocations at all. Sometimes you don't even have variables in the stack either (besides the globals).
Re: Stupid Smart Pointers in C
#118Earlier quoted context omitted.
Well, it's basically an implementation of a memory allocator. But how did you determine what you could re-use? That's the hard problem, one that's equivalent to calling free() at the right time.
When something would normally be freed, it calls the memory manager's version of free(), which zeroes the memory and adds it back into the appropriate available list.
libc already does that. What is it that yours is adding?
I'd say 25 years ago you could write your own naive allocator, make just a couple of assumptions for your use case, and beat libc. But no more.
One of the selling points of Java in the 90s was the compacting part. Because in the 90s fragmentation was a much bigger problem than it is today. Today the libc allocators have advanced by maybe tens of thousands of PhDs worth of theory and practice. Oh, and we have 64bit virtual address space, which helps with some (but not all) of the problems with memory fragmentation.
See this post from Ian Lance Taylor about why Go didn't even bother with a compacting GC: https://groups.google.com/g/golang-nuts/c/KJiyv2mV2pU?pli=1
Re: Stupid Smart Pointers in C
#119Re: Stupid Smart Pointers in C
#120this is way overkill the way i do this in C looks like initialize all resource pointers to NULL; attempt all allocations; if all pointers are non-NULL, do the thing (typically calling another routine) free all non-NULL pointers realloc(ptr, 0) nicely handles allocations and possible-NULL deallocations
might as well free the NULL pointers as well - this is totally valid C and can simplify the code
for example, the object could be managing an open file, or an open socket