News for hackers. Stuff that matters.
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by Dylan16807 on Sunday October 28, 2018 @11:56PM (#18324945)
It would help a little bit, but not enough. And if a small buffer will do the job, you can always use m[MAX].
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by burfog on Monday October 29, 2018 @12:09AM (#18324988)
m[MAX] means you can't usefully use sizeof

A really nice use for a VLA would be in the middle of a struct. You could make something like a PNG image file chunk as a struct, with the CRC at the end coming after the VLA. You get a struct of the correct size with the right fields, despite one part of the struct being of variable size.

Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by Dylan16807 on Monday October 29, 2018 @12:44AM (#18325128)
Why use sizeof when you already have n? I could see it possibly being useful but not often.
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by SamReidHughes on Monday October 29, 2018 @12:22AM (#18325035)
You’d need the VLA length encoded somewhere in the struct, though.
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by burfog on Tuesday October 30, 2018 @01:25PM (#18339089)
No you don't. It is a separate variable.

For example, a parameter n is passed to a function, and then a struct is defined based on n.

    struct {
        uint32_t header;
        uint32_t array[n];
        uint32_t footer;
    }foo;
The function fills in the struct. Maybe it then sends the struct via a packet-oriented protocol, perhaps over a datagram socket, so the length is transmitted implicitly in the packet size.
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by SamReidHughes on Tuesday October 30, 2018 @03:11PM (#18340303)
That’s different from a VLA, which doesn’t use dependent typing or any reliance on users keeping variables unmodified to guarantee they clean up the stack properly.
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by burfog on Wednesday October 31, 2018 @01:09AM (#18343504)
It is a VLA. See the second example:

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

There is no problem cleaning the stack because the compiler has numerous ways to do that. One way is to keep a copy of the original n, as passed to the function. Another way is to save the original stack pointer.

Making this work is not much harder than supporting the alloca function.

Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by SamReidHughes on Wednesday October 31, 2018 @12:31PM (#18347114)
Then you have to read part of the file, read some size fields, make a struct on the stack (for an image file??), copy the stuff you've read in already into that struct, and keep reading. And you now need to define how to pass a pointer of that struct type to helper functions for operating on a PNG.
Re:The Linux Kernel Is Now VLA (Variable-Length Array) Free by burfog on Wednesday October 31, 2018 @04:28PM (#18349018)
I mean for generating a PNG file, not reading one. It's not the best example I'm sure; it's just what popped into my head.

If you prefer, it's an Ethernet packet, with a CRC16 on the end. It is outbound.

The send() and write() functions are happy to accept a void pointer.