Live data from Hacker News

Legitimate Use of Variable Length Arrays

nullprogram.com

11–20 of 26 posts

Re: Legitimate Use of Variable Length Arrays

#11

"What’s the behavior in the VLA version when n is so large that sizeof(*identity) doesn’t fit in a size_t?" Isn't size_t supposed to be as large as to fit the cardinality of the largest type you can create?

They're trying to allocate an n·n element array. If int and size_t are 32b (typical 32-bit architectures), and n == INT_MAX, then n·n·sizeof(float) can't be represented by a size_t.

(Used · for multiplication, because HN turns stars into emphasis.)

Re: Legitimate Use of Variable Length Arrays

#12
post #7

>If there’s any risk that nmemb is too large, it must be guarded. [...] However, if median is expected to safely accommodate COPY_MAX elements, it may as well always allocate an array of this size. If it can’t, then that’s not a safe maximum. Et voilà, a library function that could have been portable between the smallest microcontroller and the largest workstation becomes restricted to a single application.

Um, no? That COPY_MAX would still need a different value between small MCU and full-blown PC.

Not to mention that using stack allocation on low-memory device is extremely dangerous... at least with malloc you get a nice diagnostic, while alloca will just do random program damage.

Re: Legitimate Use of Variable Length Arrays

#13
post #12
post #7

>If there’s any risk that nmemb is too large, it must be guarded. [...] However, if median is expected to safely accommodate COPY_MAX elements, it may as well always allocate an array of this size. If it can’t, then that’s not a safe maximum. Et voilà, a library function that could have been portable between the smallest microcontroller and the largest workstation becomes restricted to a single application.

Um, no? That COPY_MAX would still need a different value between small MCU and full-blown PC. Not to mention that using stack allocation on low-memory device is extremely dangerous... at least with malloc you get a nice diagnostic, while alloca will just do random program damage.

It does not do random damage, you get a segmentation fault in most systems.

In really small embedded you should not be allocating anything meaningful in the stack anyway.

Re: Legitimate Use of Variable Length Arrays

#14
This doesn't seem to be a new thing generally. Variable length arrays (if you like) are just what you have in Fortran. If you support scientific processing, you'll often have seen mysterious SEGVs due to people insisting on using Intel ifort, which allocates arrays on the stack by default (or used to?), and doesn't fare well with typical default stack limits. This is the equivalent of Gfortran's -fstack-arrays, which is only turned on by -Ofast. (Recursive procedures use the stack anyway.)

Re: Legitimate Use of Variable Length Arrays

#15

> Stack allocations are trivial and fast by comparison: Allocation is a matter of bumping the stack pointer, and no synchronization is needed. And this is why runtimes with moving garbage collectors can be faster than malloc - allocation with such a GC is always this cheap even when it's to the heap. (The costs move elsewhere, of course, but ideally to another thread, and are temporally and spatially grouped so that…

Very good GCs can be faster compared to common system allocators while minimizing the STW moment, but you cannot beat using a proper allocator for your needs.

Re: Legitimate Use of Variable Length Arrays

#16
post #9

Earlier quoted context omitted.

Allocation is always cheap, also with manual memory management. It's reclaiming/freeing memory which is the tricky part ;) Best approach is still to minimize dynamic allocation as much as possible. One can often get surprisingly far with an entirely static memory layout that's defined upfront at application start.

In support of flohofwoe's point: good implementations of malloc will, on the common path, just bump a pointer without synchronization. The allocation path of good manual allocator implementations and garbage collectors will probably look similar.

> In support of flohofwoe's point: good implementations of malloc will, on the common path, just bump a pointer without synchronization.

How does this work when the heap address space becomes fragmented, with randomly distributed available space and objects that are still live? You can't just bump when the next piece of memory may still be in use, and when you're not able to move memory.

Re: Legitimate Use of Variable Length Arrays

#17
post #12
post #7

>If there’s any risk that nmemb is too large, it must be guarded. [...] However, if median is expected to safely accommodate COPY_MAX elements, it may as well always allocate an array of this size. If it can’t, then that’s not a safe maximum. Et voilà, a library function that could have been portable between the smallest microcontroller and the largest workstation becomes restricted to a single application.

Um, no? That COPY_MAX would still need a different value between small MCU and full-blown PC. Not to mention that using stack allocation on low-memory device is extremely dangerous... at least with malloc you get a nice diagnostic, while alloca will just do random program damage.

Isn't that like choosing your poison? If you have a low memory device, you need to statically guarantee safety and reject oversized inputs. Crashing your embedded device may work if your are a server with a client that knows to retry another backend, but isn't an elegant solution if it disables your car's brakes.

Re: Legitimate Use of Variable Length Arrays

#18
post #9

Earlier quoted context omitted.

In support of flohofwoe's point: good implementations of malloc will, on the common path, just bump a pointer without synchronization. The allocation path of good manual allocator implementations and garbage collectors will probably look similar.

> In support of flohofwoe's point: good implementations of malloc will, on the common path, just bump a pointer without synchronization. How does this work when the heap address space becomes fragmented, with randomly distributed available space and objects that are still live? You can't just bump when the next piece of memory may still be in use, and when you're not able to move memory.

That's the uncommon path.

Re: Legitimate Use of Variable Length Arrays

#19
post #11

"What’s the behavior in the VLA version when n is so large that sizeof(*identity) doesn’t fit in a size_t?" Isn't size_t supposed to be as large as to fit the cardinality of the largest type you can create?

They're trying to allocate an n·n element array. If int and size_t are 32b (typical 32-bit architectures), and n == INT_MAX, then n·n·sizeof(float) can't be represented by a size_t. (Used · for multiplication, because HN turns stars into emphasis.)

That may be true but an array of n x n chars where n is 2^32 won't fit (well obviously) in RAM and size_t should be at least 64 bit to match (2^32 x 2^32 = 2^64).

Regardless, even without these unobtainium big machines, the cardinality of the largest array for a platform (by any number of dimensions) must be representable in size_t. A too large of an array would not fit in RAM and you should get a compile/link/out of memory error.

Re: Legitimate Use of Variable Length Arrays

#20

> Stack allocations are trivial and fast by comparison: Allocation is a matter of bumping the stack pointer, and no synchronization is needed. And this is why runtimes with moving garbage collectors can be faster than malloc - allocation with such a GC is always this cheap even when it's to the heap. (The costs move elsewhere, of course, but ideally to another thread, and are temporally and spatially grouped so that…

Very good GCs can be faster compared to common system allocators while minimizing the STW moment, but you cannot beat using a proper allocator for your needs.

> but you cannot beat using a proper allocator for your needs

How do you know this to be true?

And why isn't a GC a 'proper' allocator?

Post reply on HN