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;
}
2
  • 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++. Commented Jan 30, 2022 at 6:07
  • 1
    With GCC and similar compilers the -pedantic flag will warn if you use a compiler-specific extension. Commented Jan 30, 2022 at 8:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

If size is a constant, it's probably better practice to use std::array<int, size> numberArray;.
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: You won't get an error if you are using a compiler that supports the VLA extension, like gcc and clang do: 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.
@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).
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?
|
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.

Comments

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.