Hum responded to this 7 years after the discovery. Contrary to the answer to this question, there is hope for bold devils :).
I came across this necessity by sharing a global VLA (dyn array, etc.) in a multi-threaded application. A short story, I need my thread to share the global array, I will postpone synchronization / cache issues here, since I just want to show how to use VLA, this example can be output for other needs (for example, for an external VLA, etc. ...)
Here is the code, followed by an annotation explaining why this works.
#include <pthread.h>
(* 1) Here we declare a VLA, the execution line will determine the number of threads, as well as the size of our 2 dim VLA, n lines (1 per thread) with m values โโin each.
(* 2) We declare (en setup) our global VLA, we expose our global n and m (like gn, gm) and our global array as a pointer to the scalar type of the array (here int), we start to point to a [0 ] [0].
(* 3) we set the values โโto [n] [m] (sequentially int, 0, 1, 2, ...)
(* 4) All threads are started using init_thread (), note that we declare a dummy array of the same type as our [n] [m] VLA, the goal here is to pass an array compatible with our worker () API.
(* 5) Our employee needs a long type for n, m (dim), this is explained in (* 6), so here we pass global n and m to our processed and dummy array, we donโt care about that, the only goal is to pass the array addr as an argument.
(* 6) A working API, we have several arguments (for example, tndx), then we have a VLA, denoted by long, n, long m, int a [n] [m]. At this point, a [] [] is x [] [] and is not useful.
We used long for n and m to correct the unexpected appearance of the stack, then n, m and a are glued together, because we take addr from n and m, that is, the arguments that are in the register (modern arch) are pushed onto the stack in their placeholders , I = mp = np, take care of determining the direction of the stack (arg0, arg1, arg2), at this moment we can access the base address x [] [] and put our global ga there * ap = (long) ha;
(* 8) Now our employees can elegantly access the global (shared) VLA.
Here is a run
VY$ cc -o t2 t2.c -lpthread VY$ ./t2 3 4 Init a[][] a[0][0]=0 a[0][1]=1 a[0][2]=2 a[0][3]=3 a[1][0]=4 a[1][1]=5 a[1][2]=6 a[1][3]=7 a[2][0]=8 a[2][1]=9 a[2][2]=10 a[2][3]=11 thread #0 started worker 0 started thread #2 started worker 2 started thread #1 started worker 1 started Draining threads Final a[][] a[0][0]=0 a[0][1]=1 a[0][2]=2 a[0][3]=3 a[1][0]=1004 a[1][1]=1005 a[1][2]=1006 a[1][3]=1007 a[2][0]=2008 a[2][1]=2009 a[2][2]=2010 a[2][3]=2011
Each thread changed its line by adding ID * 1000.
In this way, we can definitely determine the globality of VLA.
VLA is cool except for the need for the student to read about alloca (), etc., but there is a need for a global one, and as explained at compile time, this is not possible, but GCC (libgcc?) Should be able to offer an API "fix" VLA base address at run time.
Iโm now that many will speak out against taking arg addr, hacking the direction of the stack, etc., but many other codes work like this: va_args, alloca, etc. So it may look ugly, but this ugliness can be hidden .
Hooray fi