1

In C, because of the framework I use and generate though a compiler, I am required to use global variable length array. However, I can not know the size of its dimension until runtime (though argv for example). For this reason, I would like to declare a global variable length array with unknown size and then define its size.

I have done it like that :

int (*a)[]; //global variable length array
int main(){
  //defining it's size
  a = (int(*)[2]) malloc(sizeof(int)*2*2);

  for(int i=0;i<2; i++){
      for(int j=0;j<2; j++){
          a[i][j] = i*2 + j;
      }
  }
  return 0;
}

However, this does not work : I get the invalid use of array with unspecified bounds error. I suspect it is because even if its size is defined, its original type does not define the size of the larger stride.

Does someone know how to solve this issue ? Using C99 (no C++) and it should be quite standard (working on gcc and icc at least).

EDIT: I may have forget something that matters. I am required to propose an array that is usable through the "static array interface", I mean by that the multiple square bracket (one per dimension).

15
  • 1
    If you're going to malloc it anyway, what's the point of the VLA? Just use a pointer. Commented Feb 2, 2018 at 7:24
  • @DeiDei the VLA is for the second dimension. It is a two dimensional array. Commented Feb 2, 2018 at 7:24
  • 1
    You cannot do what you need, use an array of pointers if this is absolutely necessary. Commented Feb 2, 2018 at 7:27
  • You cannot use a variably-modified type (a VLA) for a variable defined at file scope, nor in a structure or union, or ... You are not allowed to do what you are trying to do. You will have to go with a simple pointer and a length, maybe in a structure: int *a = 0; int a_size = 0; and then assign values appropriately. (Or an array of pointers — int **a = 0;) Commented Feb 2, 2018 at 7:27
  • You can declare a void* and a size variable though and then cast the void* to an appropriate size VLA every time you have to use. Commented Feb 2, 2018 at 7:28

2 Answers 2

2

First a is not an array but a pointer to an array of unspecified length. What you are trying to do is not possible. You can't have a global variable length array.
But with the present scenario you can use this to access the memory allocated to a

for(int i=0;i<2; i++){
    int *ptr = *a + 2*i;
    for(int j=0;j<2; j++){
        ptr[j] = i*2 + j;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, sorry for the terminology. Hum, this is not possible according to the standard ?
@Viridya; Yes. It's not possible as per C standard.
0

You cannot declare a global multi dimensional VLA, because even if you use pointers, all the dimensions except for the first one must be known at declaration time.

My best attempt would be to use a global void *. In C void * is a special pointer type that can be used to store a pointer to any type, and is often used for opaque pointers.

Here you could do:

void *a; // opaque (global variable length array) pointer 
int main() {
    //defining it's size
    a =  malloc(sizeof(int) * 2 * 2);  // the global opaque pointer
    int(*a22)[2] = a;                  // a local pointer to correct type
    for (int i = 0; i<2; i++) {
        for (int j = 0; j<2; j++) {
            a22[i][j] = i * 2 + j;
        }
    }
    return 0;
}

When you need to access the global VLA, you assign the value of the opaque global to a local VLA pointer, and can then use it normally. You will probably have to store the dimensions in global variables too...

1 Comment

int(*a22)[2] is a pointer to a fixed size array, not VLA!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.