63 points · by bwidlar · 2y ago · 59 comments
It may compile, but it might not link. With very large static arrays, in the link step, you might get something like this:
my_program.o: In function `my_function':
my_program.c:(.text+0xf38): relocation truncated to fit: R_X86_64_PC32 against `.bss'
It's due to the linker assuming statically allocated things can be addressed with a 4-byte pointer (I encountered this on x86_64 arch). Using malloc to allocate the array will fix this.That being said, using malloc once to allocate a very large array will get the same dynamic allocation of backing RAM as described in the article on linux, assuming overcommit is enabled.
I don't know what Windows does.
Windows does not allow overcommit. The commit limit is the total size of RAM + the size of the page file, attempting to commit more than that will fail.
I haven’t run it myself but I’ve heard others mention that you can prob get several hundred MB to a GB or so but shouldn’t count in getting near the 4GB limit.
Nonetheless, that’s plenty for my use case and random hacking.
(I’m actually somewhat conflicted about TFA’s idea, for what it’s worth, I just don’t think freeing memory is the most problematic part of it.)
Most programmers may not have touched those APIs but that doesn't mean that they didn't have a need for them.
And you save on having to write some kind of allocator for it.
To a degree, I agree with the premise of the post, we can preallocate huge chunks, and actual resource consumption will only happen on demand when the memory is first used. But note that there is still a resource whose allocation we can't delay: virtual address space, which must get acquired immediately obviously. When trying to be compatible with 32-bit machines (less than 4GB of virtual address space available), preallocating huge chunks is not practical.
As to using static globals, I'm not sure it's the best idea. One can use mmap() (or Virtual Alloc() on Windows) to the same effect. That might be better from an architectural view, with regards to encapsulation and such, and allows a little more dynamicity, making multiple separate (but still large) allocations and such. It's possible to give debugger-visible names to these allocations as well, for example by way of the memfd_create() API.
An error indicates the user's fault: they need to correct their input before retrying.
An apology indicates the program's fault: either logic for what had been incorrectly thought to be a rare^2 case is missing, or the program has only been configured to deal with N foos, and the user's input requires more. In the former case, the user needs to get a programmer fix before retrying, but in the latter, maybe they can just reconfigure and rebuild (even better, restart with different env vars?) then retry.
The relevant point of this is that when the user is likely to be another programmer, "reconfigure" can be as simple as editing a static allocation.
However, many programs require a bit more planning how to deal with unfortunate situations, at least without entirely restarting. It's good to not prevent repairing an unfortunate situation from the start by baking in certain constants that can't be practically assumed.
Better than editing a static allocation, is if the program can cope with that situation automatically in some way. Next better thing, have it runtime configurable (e.g. command line arguments, no recompilation needed). I don't see a huge benefit to prefer global arrays over memory maps generally.
This would mean a more complicated malloc, where you ask for both an actual memory allocation, and an amount of address space to be allocated. And will still require you to have some sense of the maximum size you might need in a given vector. Also, it forces memory allocations to be multiples of the page size, which isn't always ideal.
[0] https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/cheri...
Use request at creation to set a capacity for 99.9% of use cases. Still have realloc there to stop any sneaky buffer overflows.
Have your cake and eat it too!
But if we had let the vector grow naturally it would probably not make such a big realloc()
There's a lot of problems that can be solved just fine with an upfront defined fixed memory layout in a global static variable.
Dismissing globals is basically the same problem as sweeping generalizations like "goto considered harmful", "avoid for-loops" or "almost always auto" - such statements can serve as advice for newbies, but following the advice blindly can also lead to less elegant and less maintainable solutions.
...or from a function that calls itself, directly or indirectly. This automatically becomes possible basically whenever the function accesses a user-supplied callback. So you'll run into issues unless you have full control over your callees, or only access the global array in logically-atomic patterns.
(To give an ancient example: to enable function calls, PDP-8 programs allocated a word of static memory before the top of each subroutine to store the return address. However, if a subroutine tried to recurse into itself, then the old return address would be overwritten, and it would never be able to return properly. Supposedly this could result in very gnarly errors to debug.)
Programming languages should directly support a data structure which is guaranteed to have this behavior. Not just that it allocates a new array which is 4k bigger and the copies it over. But one which grows by having the OS map another 4k block to have the addresses past the old end of the array.
I made a small experiment with 1-million int array, which has a single element written to at index 100000, and then force a core dump by writing to a dereferenced int pointer which is initialized as 0.
The resulting core file is almost 4gigabytes.
https://www.merriam-webster.com/grammar/people-vs-persons
Then again, I'm not a language prescriptivist. I post the above only to demonstrate that the use of "persons" is hardly unusual.