Home > other >  How do I tell if I am using VLA (Variable Length Array)?
How do I tell if I am using VLA (Variable Length Array)?

Time:01-30

I am on a project where we have to read in from a file, temporarily store them in dynamically allocated memory, do sorting and stuff, and deallocate the memory.

As per the project is testing our knowledge over dynamic memory and memory leak, one of the instructions is do not use VLA.

I am not sure what our instructor means by we should not use VLA, are we not allowed use [] bracket syntax? or we can use them as long as we use memories from heap and deallocate them properly once they are of no use anymore.

Here is my main.cpp, it is not complete yet, so please excuse some typos and possible errors, but if you have something to suggest or correct, those are more than welcome as well.

Thank you and do have a good weekend y'all.

#include "proj2-arrayFunctions.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    ifstream file;
    int size = 0;
    int* numberArray;
    int counter = 0;

    file.open("arrays.txt");

    if (!file)
    {
        cout << "error: file is not opened! " << endl;
        return 1;
    }

    while(file.good())
    {
        file >> size;
        numberArray = new int[size];
        for (int i = 0; i < size; i  )
        {
            file >> numberArray[i];
        }
        bubbleSort(numberArray, size);

        cout << "the largest value from this array is: " << largestValue(numberArray, size) << endl;
        cout << "the smallest value from this array is: " << smallestValue(numberArray, size) << endl;
        cout << "the average value of this array is: " << averageValue(numberArray, size) << endl;
        cout << "the median value of this array is: " << medianValue(numberArray, size) << endl;

        delete[] numberArray;
    }
    
    return 0;
}

CodePudding user response:

int *numberArray; numberArray = new int[size]; is not a variable-length array, it's a dynamically allocated array. That's fine. Note that you have to delete[] it when done, which you do.

A VLA declaration would look like int numberArray[size]; where size is not a constant. It gets automatically deallocated when it goes out of scope, so you don't use delete on it. They are typically allocated on the stack and so can be created and deallocated very fast, but have various pitfalls. The main one is that there is no way to check if enough stack space is available, and your program will simply crash if there isn't; there is no way to safely detect or handle that error. So you would have to be very careful about checking that the value of size is reasonable.

VLAs are not part of standard C , but some compilers support them anyway.

CodePudding user response:

If you want to avoid using VLAs, use the appropriate compiler flag to treat VLAs as errors, which would be -Werror=vla for GNUC (gcc, clang, icc, et cetera). MSVC doesn't support VLAs anyway.

  •  Tags:  
  • Related