2

I want to create a variable length array for my code in the Visual Studio 2010 environment.

I had tried the code using the array of length x, as it is passing by the user. But I am facing the error as:

"error C2466:cannot allocate an array of constant size 0" ,"error C2133: 'v_X_array' : unknown size".

func1(int x)
{
    int v_X_array[x];
    int i;
    for (i=0; i<x; i++)
    {
        v_X_array[i] = i;
    }
}

I expect the answer as v_X_array[0] = 0, v_X_array[1] =1, v_X_array[2]=2 ... v_X_array[10]=10 ; for x = 10;

How can I do this?

Note: as calloc and malloc should not be used.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tejas
  • 55
  • 8
  • 1
    You might try to use different settings for C standard version. But according to [Lundin's answer to this question](https://stackoverflow.com/a/49028959/6782754), I would not expect too much. VLA are a feature of C99 and even became optional with C11. VS is not known for outstanding standard conformance. – Gerhardh May 29 '19 at 05:52
  • 1
    You can create a int pointer like `int *array` then use for loop to assign some value to the pointer will work same for you. – Vaibhav May 29 '19 at 05:53
  • 1
    @Vaibhav Using a pointer would require allocating some memory. That is tricky given that `malloc` and `calloc` shall not be used. – Gerhardh May 29 '19 at 05:55
  • What about `_alloca()`? – Shawn May 29 '19 at 06:02
  • No,, @Vaibhav whaen I am using pointer program get terminated.. – Tejas May 29 '19 at 06:19
  • 1
    How do you use the pointer? Why can't you use `malloc` or `calloc`? – Gerhardh May 29 '19 at 06:56
  • 1. x should be `unsigned` or better `size_t` and `const` because is not change in function. 2. After validate you should use `malloc` and `free` after operations. I mean validate so you must provide max expected length, and after malloc check `v_X_array != NULL` pointer after malloc is NULL when there are not enought memory to allocate. – Igor Galczak May 29 '19 at 07:50
  • 1
    You need a compiler from 1999 or later. VS 2010 is far older than that. I guess the number 2010 stands for the number of ISO 9899:1999 violations and not the year of release. – Lundin May 29 '19 at 09:15
  • Possible duplicate of [Enabling VLAs (variable length arrays) in MS Visual C++?](https://stackoverflow.com/questions/5246900/enabling-vlas-variable-length-arrays-in-ms-visual-c) – Diodacus May 30 '19 at 10:30

2 Answers2

0

If you need your code to be portable, you cannot use that kind of array definition to handle memory areas.

Without going into specific implementations, you have two generic approaches that you can use:

  1. Define an array big enough for the worst case. This is tightly dependent on the application, so you are on your own.
  2. Define the "array" using dynamic allocation. With that, you can define memory areas of any arbitrary size.

If you choose option 2:

a. Do not forget to de-allocate the memory when you no longer need it.

b. To avoid frequent allocation and de-allocation, you may define the buffer once (perhaps bigger then necessary for the current call) and use it several times. You may and up with the same result as option 1 above - define a large array from the start.


Since you should not use dynamic allocation ("calloc and malloc should not be used"), then you are left with option 1.


I expect the ans as v_X_array[0] = 0, v_X_array[1] =1, v_X_array[2]=2 ... v_X_array[10]=10 ; for x = 10;

You expect to store 11 values in an array which can hold only 10?

virolino
  • 2,073
  • 5
  • 21
  • If you need your C code to be portable, you cannot use MSVC. That's a bit strongly stated, but MSVC simply is not a conforming C compiler. See https://stackoverflow.com/questions/51960833/visual-studio-2017-does-not-supportc11-new-feature-generic among many others documenting MSVC's lack of conformance to C standards. – Andrew Henle Jun 06 '19 at 12:02
  • @AndrewHenle: In my mind, I have the distinction between the code I write ("my code") and everything else done by the system (in this case MSVC). I agree that the project itself might not be portable, but some fragments can. Example (my case): I prefer to train myself in writing portable code always. I write non-portable code only when I have no alternative. – virolino Jun 06 '19 at 12:08
0

You can't allocate an array of an unknown size. So you need to allocate it dynamically "at run-time". you can make this allocation using "new" in C++ or "malloc" in C.

For example:

In C++ if you want to allocate an array of an unknown size you should do the following:

int* v_X_array = new int[x];
int i;
for (i=0; i<x; i++)
{
    v_X_array[i] = i;
}

The reason that we use integer pointer is that "new" returns the base address of the array "the address of the first element", so the only thing that can store addresses is pointers.

In C if you want to allocate an array of an unknown size you should do the following:

    int* v_X_array = (int*) malloc(x*sizeof(int));
    int i;
    for(i=0; i<x; i++)
    {
        v_X_array[i] = i;
    }

The malloc function takes a single argument which specifies the number of bytes to be allocated and returns a void pointer so the casting (int*) is required.

For more explanations, look at the next section:

If we need to allocate an array of 20 integers it could be as follow: "malloc(20*sizeof(int))" where 20 is the number of allocated elements and sizeof(int) is the size of the type you want to allocate. If successful it returns a pointer to memory allocated. If it fails, it returns a null pointer.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fouad
  • 39
  • 3
  • 1
    Support for Variable-length arrays [is mandatory in C per the C99 standard](https://port70.net/~nsz/c/c99/n1256.html#6.7.5.2). – Andrew Henle Jun 06 '19 at 12:07
  • @AndrewHenle: What does the 1999 C standard have to do with a question asked in 2019 about Visual Studio 2010, which did not support the C standard? – Eric Postpischil Aug 31 '20 at 15:25