Run-Time Check Failure #2 – Stack around the variable ‘sortObject’ was corrupted. how to fix?

  c++

I was trying to store numbers in an array. The first half of the array are numbers that are ascending 1,2,3,4,5 etc and the second half of the array are random numbers. When i run the program it produces the output I wanted but gives me the error please help

 #include <iostream>
 #include <cstdlib>
 using namespace std;

 class sorting {
 private:
int size, elements;
int arr[NULL];


public:
void sort(){
    cout << "Enter number of desired elements" << ">"; cin >> elements;
    arr[elements];
    half();
    

}
void half() {
    for (int i = 0; i < elements/2; i++) {
        arr[i] = i + 1;
    }
    for (int i = elements / 2; i < elements; i++) {
        arr[i] = rand();
    }
    cout << "This is the elements of the array";
    for (int i = 0; i < elements; i++) {
        cout << arr[i] << " ";
    }
}
};
   int main()
{
sorting sortObject;

sortObject.sort();
return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT