1

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;
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    You have a very smart instructor. Consider yourself fortunate, there's been endless evidence posted to Stackoverflow, over the years, of incompetent C++ instructors causing endless frustrations for people who actually wan to learn and understand C++. – Sam Varshavchik Jan 30 '22 at 06:07
  • 1
    With GCC and similar compilers the `-pedantic` flag will warn if you use a compiler-specific extension. – user4581301 Jan 30 '22 at 08:30

2 Answers2

5

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.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 1
    _If_ `size` is a constant, it's probably better practice to use `std::array numberArray;`. – Chris Jan 30 '22 at 05:56
  • I believe you will get a compile time error in `int numberArray[size];` if `size` is not a constant. And IMHO the instructor meant `std::vector` by VLA. – LPVOID Jan 30 '22 at 05:59
  • 3
    @LPVOID: You won't get an error if you are using a compiler that supports the VLA extension, like gcc and clang do: https://godbolt.org/z/PazEj6q4P. If the instructor said "variable-length array" when they meant "vector", then they are pretty confused; "variable-length array" has a well-established meaning in C that has carried over to C++, and I would assume that if they go to the trouble to use that exact phrase, then they intend its precise meaning. – Nate Eldredge Jan 30 '22 at 06:03
  • @LPVOID Visual C++, the major compiler for Windows, does not support, never did support, and will never support, variable length arrays (unless they make it into the C++ standard). – PaulMcKenzie Jan 30 '22 at 06:13
  • It is not a vector that is restricted to use; its a variable length array. and could you further explain why I should put const for size? – ThomTheGreat Jan 30 '22 at 09:03
  • In your example size is non-const (you read it from a file). Then you would typically use new int[] or vector. There are situations, where a const-sized array is needed/used in a program. In those situations the constructs above can be used, or just int[size] or often best array. – Sebastian Jan 30 '22 at 10:33
5

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93